夜雨飘零
2023-09-14 91f614934fb92765ca2d4166a416ab354769320f
Support for multiple processes (#951)

3个文件已修改
2个文件已添加
94 ■■■■■ 已修改文件
funasr/runtime/python/http/README.md 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/python/http/asr_nginx.conf 44 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/python/http/client.py 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/python/http/server.py 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/python/http/start_server.sh 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/python/http/README.md
@@ -45,3 +45,22 @@
--add_pun [add pun to result] \
--audio_path [use audio path] 
```
## 支持多进程
方法是启动多个`server.py`,然后通过Nginx的负载均衡分发请求,达到支持多用户同时连效果,处理方式如下,默认您已经安装了Nginx,没安装的请参考[官方安装教程](https://nginx.org/en/linux_packages.html#Ubuntu)。
配置Nginx。
```shell
sudo cp -f asr_nginx.conf /etc/nginx/nginx.conf
sudo service nginx reload
```
然后使用脚本启动多个服务,每个服务的端口号不一样。
```shell
sudo chmod +x start_server.sh
./start_server.sh
```
**说明:** 默认是3个进程,如果需要修改,首先修改`start_server.sh`的最后那部分,可以添加启动数量。然后修改`asr_nginx.conf`配置文件的`upstream backend`部分,增加新启动的服务,可以使其他服务器的服务。
funasr/runtime/python/http/asr_nginx.conf
New file
@@ -0,0 +1,44 @@
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend {
        # 最少连接算法
        least_conn;
        # 启动的服务地址
        server localhost:8001;
        server localhost:8002;
        server localhost:8003;
    }
    server {
        # 实际访问的端口
        listen 8000;
        location / {
            proxy_pass http://backend;
        }
    }
    include /etc/nginx/conf.d/*.conf;
}
funasr/runtime/python/http/client.py
@@ -1,3 +1,5 @@
import os
import requests
import argparse
@@ -32,7 +34,7 @@
url = f'http://{args.host}:{args.port}/recognition'
data = {'add_pun': args.add_pun}
headers = {}
files = [('audio', ('file', open(args.audio_path, 'rb'), 'application/octet-stream'))]
files = [('audio', (os.path.basename(args.audio_path), open(args.audio_path, 'rb'), 'application/octet-stream'))]
response = requests.post(url, headers=headers, data=data, files=files)
print(response.text)
funasr/runtime/python/http/server.py
@@ -1,8 +1,7 @@
import argparse
import logging
import os
import random
import time
import uuid
import aiofiles
import ffmpeg
@@ -92,7 +91,7 @@
async def api_recognition(audio: UploadFile = File(..., description="audio file"),
                          add_pun: int = Body(1, description="add punctuation", embed=True)):
    suffix = audio.filename.split('.')[-1]
    audio_path = f'{args.temp_dir}/{int(time.time() * 1000)}_{random.randint(100, 999)}.{suffix}'
    audio_path = f'{args.temp_dir}/{str(uuid.uuid1())}.{suffix}'
    async with aiofiles.open(audio_path, 'wb') as out_file:
        content = await audio.read()
        await out_file.write(content)
@@ -105,6 +104,7 @@
    if add_pun:
        rec_result = inference_pipeline_punc(text_in=rec_result['text'], param_dict={'cache': list()})
    ret = {"results": rec_result['text'], "code": 0}
    print(ret)
    return ret
funasr/runtime/python/http/start_server.sh
New file
@@ -0,0 +1,21 @@
#!/bin/bash
# 创建日志文件夹
if [ ! -d "log/" ];then
  mkdir log
fi
# kill掉之前的进程
server_id=`ps -ef | grep server.py | grep -v "grep" | awk '{print $2}'`
echo $server_id
for id in $server_id
do
    kill -9 $id
    echo "killed $id"
done
# 启动多个服务,可以设置使用不同的显卡
CUDA_VISIBLE_DEVICES=0 nohup python -u server.py --host=localhost --port=8001 >> log/output1.log 2>&1 &
CUDA_VISIBLE_DEVICES=0 nohup python -u server.py --host=localhost --port=8002 >> log/output2.log 2>&1 &
CUDA_VISIBLE_DEVICES=0 nohup python -u server.py --host=localhost --port=8003 >> log/output3.log 2>&1 &