Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
eshiled-new
/
funasr_web
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit b8ecc450
authored
Mar 24, 2024
by
‘chizw’
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[+]进程池优化
1 parent
b520263d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
68 additions
and
21 deletions
funasr_service.py
kafka_service/funasr_consumer.py
mysql_service/funasr_db.py
mysql_service/mysql_pool.py
funasr_service.py
View file @
b8ecc45
...
...
@@ -2,12 +2,10 @@ import multiprocessing
import
threading
import
time
from
funasr
import
AutoModel
import
kafka_service.funasr_producer
from
kafka_service
import
funasr_producer
from
log.logger
import
log
import
json
import
traceback
from
mysql_service
import
funasr_db
# paraformer-zh is a multi-functional asr model
...
...
@@ -18,10 +16,13 @@ model = AutoModel(
vad_model
=
"fsmn-vad"
,
punc_model
=
"ct-punc-c"
,
spk_model
=
"cam++"
,
ncpu
=
8
,
ncpu
=
4
,
device
=
"cpu"
,
batch_size_s
=
100
,
batch_size_threshold_s
=
60
*
60
# model="iic/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-pytorch",
# vad_model="iic/speech_fsmn_vad_zh-cn-16k-common-pytorch",
# punc_model="iic/punc_ct-transformer_cn-en-common-vocab471067-large",
)
...
...
@@ -99,8 +100,10 @@ def deal_worker(task_id: str):
or
exception_msg
==
"local variable 'raw_text' referenced before assignment"
:
funasr_db
.
update_ali_asr_model_res_skip
(
task_id
)
return
funasr_db
.
update_process_task
(
task_id
,
str
(
multiprocessing
.
current_process
()))
url
=
sql_res
[
0
][
"file_url"
]
if
url
==
""
or
len
(
url
)
<
1
or
not
url
.
startswith
(
"http"
):
funasr_db
.
update_ali_asr_model_res_skip
(
task_id
)
return
log
.
info
(
f
"Worker {task_id} is running... url:{url}"
)
consuming_start_time
=
time
.
perf_counter
()
...
...
@@ -111,8 +114,8 @@ def deal_worker(task_id: str):
funasr_db
.
update_ali_asr_model_res
(
task_id
,
str
(
output_res
),
0
)
return
sentence_info
=
output_res
[
0
][
"sentence_info"
]
# log.info("output_res.sentence_info: %s" % sentence_info)
json_output
=
fine_grained_transform_output
(
sentence_info
)
# log.info("json_output: %s" % json_output)
execute_time
=
time
.
perf_counter
()
-
consuming_start_time
funasr_db
.
update_ali_asr_model_res
(
task_id
,
json_output
,
int
((
execute_time
*
1000
)))
log
.
info
(
...
...
@@ -122,7 +125,7 @@ def deal_worker(task_id: str):
except
Exception
as
e
:
log
.
error
(
f
"Worker error:{traceback.format_exc()}"
)
funasr_db
.
update_ali_asr_model_res_fail
(
task_id
,
str
(
e
),
traceback
.
format_exc
())
kafka_service
.
funasr_producer
.
send_task_id
(
task_id
)
funasr_producer
.
send_task_id
(
task_id
)
def
fine_grained_transform_output
(
sentence_info
):
...
...
kafka_service/funasr_consumer.py
View file @
b8ecc45
...
...
@@ -9,6 +9,8 @@ from mysql.connector import pooling
from
config.config
import
ConfigInfo
from
concurrent.futures
import
ThreadPoolExecutor
from
kafka.structs
import
TopicPartition
from
mysql_service
import
funasr_db
from
kafka_service
import
funasr_producer
# 配置 Kafka 消费者
consumer_new
=
KafkaConsumer
(
...
...
@@ -19,10 +21,12 @@ consumer_new = KafkaConsumer(
)
log
.
info
(
"启动2"
)
class
MultiThreadKafka
(
object
):
def
__init__
(
self
):
self
.
seek
=
0
# 偏移量
def
start_consumer
(
self
,
consumer
):
log
.
info
(
f
"启动consume_kafka Thread name:{threading.current_thread().name}"
)
# 消费消息并进行逻辑处理
...
...
@@ -66,12 +70,13 @@ def multi_thread_consumer():
# 循环消费消息
def
consume_kafka
():
log
.
info
(
f
"启动consume_kafka Thread name:{threading.current_thread().name}"
)
# 消费消息并进行逻辑处理
try
:
process_pool
=
multiprocessing
.
Pool
(
processes
=
2
)
process_pool
=
multiprocessing
.
Pool
(
processes
=
2
,
initializer
=
consumer_process_init
)
for
message
in
consumer_new
:
if
message
is
None
:
continue
...
...
@@ -80,7 +85,8 @@ def consume_kafka():
# funasr_service.handle_process(str(message.value.decode('utf-8')))
arg
=
str
(
message
.
value
.
decode
(
'utf-8'
))
log
.
info
(
f
"arg:{arg},process_pool1:{process_pool}"
)
process_pool
.
apply_async
(
funasr_service
.
handle_process
,
(
arg
,),)
process_pool
.
apply_async
(
func
=
funasr_service
.
handle_process
,
arg
=
(
arg
,),
callback
=
None
,
error_callback
=
consumer_process_error_callback
)
# process = multiprocessing.Process(target=funasr_service.handle_process, args=((str(message.value.decode('utf-8'))),),)
# process.start()
print
(
"task handle"
)
...
...
@@ -89,9 +95,28 @@ def consume_kafka():
except
Exception
as
e
:
log
.
error
(
"funasr consumer Exception: "
+
str
(
traceback
.
format_exc
()))
# 循环消费消息
# for message in consumer:
# log.info(f"Received message: {message.value}")
# if message is None:
# continue
# funasr_service.handle_process(str(message.value.decode('utf-8')))
def
consumer_process_init
():
try
:
process_name
=
str
(
multiprocessing
.
current_process
())
log
.
info
(
f
"consumer_process_init process_name:{process_name}"
)
res
=
funasr_db
.
select_process_fail_task
(
process_name
)
if
res
is
not
None
:
funasr_producer
.
send_task_id
(
res
[
0
][
"task_id"
])
except
Exception
as
e
:
log
.
error
(
f
"consumer_process_init_error process_name:{process_name}"
,
e
)
def
consumer_process_callback
():
pass
def
consumer_process_error_callback
(
e
):
log
.
error
(
f
"consumer_process_error_callback:"
,
e
)
mysql_service/funasr_db.py
View file @
b8ecc45
...
...
@@ -35,30 +35,46 @@ def update_ali_asr_model_res(task_id: str, json_output: str, execute_time: int):
def
update_ali_asr_model_res_fail
(
task_id
:
str
,
exception_msg
:
str
,
exception
:
str
):
update_sql
=
(
f
"update ali_asr_model_res t set t.task_status = 2, t.exception_msg =
%
s, t.exception_log =
%
s, "
f
"exception_limit = exception_limit + 1 "
f
"where t.task_id =
%
s;"
)
"update ali_asr_model_res t set t.task_status = 2, t.exception_msg =
%
s, t.exception_log =
%
s, "
"exception_limit = exception_limit + 1 "
"where t.task_id =
%
s;"
)
return
pool
.
update_one
(
update_sql
,
(
exception_msg
,
exception
,
task_id
,))
def
update_ali_asr_model_res_skip
(
task_id
:
str
):
update_sql
=
(
f
"update ali_asr_model_res t set t.task_status = 2,"
f
"exception_limit = exception_limit + 1 "
f
"where t.task_id =
%
s;"
)
"update ali_asr_model_res t set t.task_status = 2,"
"exception_limit = exception_limit + 1 "
"where t.task_id =
%
s;"
)
return
pool
.
update_one
(
update_sql
,
(
task_id
,))
def
select_ali_asr_model_res
(
task_id
:
str
):
select_sql
=
(
f
"select id,task_id,file_url,task_status,output_data,exception_msg"
f
" from ali_asr_model_res aamr"
f
" where del_flag = 0 and "
f
"task_id =
%
s and exception_limit < 3;"
)
"select id,task_id,file_url,task_status,output_data,exception_msg"
" from ali_asr_model_res aamr"
" where del_flag = 0 and "
"task_id =
%
s and exception_limit < 3;"
)
return
pool
.
select_all
(
select_sql
,
(
task_id
,))
def
select_ali_asr_model_wait
():
select_sql
=
(
f
"select id,task_id,file_url,task_status,output_data from ali_asr_model_res aamr where aamr.del_flag = 0 and "
f
"aamr.output_data is null and aamr.task_status !=
%
s and exception_limit < 3;"
)
"select id,task_id,file_url,task_status,output_data from ali_asr_model_res aamr where aamr.del_flag = 0 and "
"aamr.output_data is null and aamr.task_status !=
%
s and exception_limit < 3;"
)
return
pool
.
select_all
(
select_sql
,
1
)
def
select_process_fail_task
(
process_name
:
str
):
select_sql
=
(
"select id,task_id,file_url,task_status from ali_asr_model_res aamr where aamr.del_flag = 0 "
"and aamr.task_status = 0 and aamr.execute_process_name =
%
s;"
)
return
pool
.
select_all
(
select_sql
,
(
process_name
))
def
update_process_task
(
task_id
:
str
,
process_name
:
str
):
update_sql
=
(
"update ali_asr_model_res t set t.execute_process_name =
%
s where task_id =
%
s;"
)
return
pool
.
update_one
(
update_sql
,
(
process_name
,
task_id
))
mysql_service/mysql_pool.py
View file @
b8ecc45
...
...
@@ -51,7 +51,10 @@ class ConnPool:
result
=
cursor
.
fetchall
()
self
.
close
(
conn
,
cursor
)
log
.
info
(
f
"execute_select_all result:{result}"
)
return
result
if
str
(
result
)
==
"()"
:
return
None
else
:
return
result
def
insert_one
(
self
,
sql
,
args
):
"""插入单条数据"""
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment