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 05702077
authored
Mar 20, 2024
by
‘chizw’
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[+]多线程消费
1 parent
f3d8787a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
14 deletions
kafka_service/funasr_consumer.py
main.py
kafka_service/funasr_consumer.py
View file @
0570207
...
...
@@ -2,16 +2,16 @@ import multiprocessing
import
threading
import
time
import
traceback
from
kafka
import
KafkaConsumer
import
funasr_service
from
kafka
import
KafkaConsumer
from
log.logger
import
log
from
mysql.connector
import
pooling
from
config.config
import
ConfigInfo
from
concurrent.futures
import
ThreadPoolExecutor
from
kafka.structs
import
TopicPartition
# 配置 Kafka 消费者
consumer
=
KafkaConsumer
(
consumer
_new
=
KafkaConsumer
(
ConfigInfo
.
kafka_consumer_analysis_topic
,
bootstrap_servers
=
ConfigInfo
.
kafka_consumer_bootstrap_servers
,
# Kafka broker 的地址
group_id
=
ConfigInfo
.
kafka_consumer_group_id
,
# 消费者组 ID
...
...
@@ -56,16 +56,67 @@ def execute_sql(sql):
conn
.
close
()
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}"
)
# 消费消息并进行逻辑处理
try
:
for
message
in
consumer
:
if
message
is
None
:
continue
log
.
info
(
f
"Received message: {message}"
)
log
.
info
(
f
"""process name:{multiprocessing.current_process()},thread name:{threading.current_thread().name},
Received message value: {message.value}"""
)
# 处理逻辑
start_time
=
time
.
time
()
funasr_service
.
handle_process
(
str
(
message
.
value
.
decode
(
'utf-8'
)))
print
(
"task handle"
)
end_time
=
time
.
time
()
log
.
info
(
"handle_process耗时:"
+
str
(
end_time
-
start_time
))
except
Exception
as
e
:
traceback
.
print_exc
()
log
.
error
(
"funasr consumer Exception: "
+
str
(
e
))
def
operate
(
self
):
consumer
=
KafkaConsumer
(
ConfigInfo
.
kafka_consumer_analysis_topic
,
bootstrap_servers
=
ConfigInfo
.
kafka_consumer_bootstrap_servers
,
# Kafka broker 的地址
group_id
=
ConfigInfo
.
kafka_consumer_group_id
,
# 消费者组 ID
auto_offset_reset
=
ConfigInfo
.
kafka_consumer_auto_offset_reset
,
# 从最早的消息开始消费
)
tp
=
TopicPartition
(
ConfigInfo
.
kafka_consumer_analysis_topic
,
0
)
consumer
.
assign
([
tp
])
# consumer.seek(tp, self.seek)
self
.
seek
+=
1
# consumer_data = next(consumer)
self
.
start_consumer
(
consumer
)
def
main
(
self
):
thread_pool
=
ThreadPoolExecutor
(
max_workers
=
2
,
thread_name_prefix
=
"funasr_"
)
# 我们使用线程池统一管理线程
for
i
in
range
(
2
):
thread_pool
.
submit
(
self
.
operate
,
)
def
multi_thread_consumer
():
thread_kafka
=
MultiThreadKafka
()
thread_kafka
.
main
()
# 循环消费消息
def
consume_kafka
():
log
.
info
(
f
"启动consume_kafka Thread name:{threading.current
Thread()
}"
)
log
.
info
(
f
"启动consume_kafka Thread name:{threading.current
_thread().name
}"
)
# 消费消息并进行逻辑处理
try
:
for
message
in
consumer
:
for
message
in
consumer
_new
:
if
message
is
None
:
continue
log
.
info
(
f
"Received message: {message}"
)
log
.
info
(
f
"process name:{multiprocessing.current_process()},thread name:{threading.currentThread()},Received message value: {message.value}"
)
log
.
info
(
f
"""process name:{multiprocessing.current_process()},thread name:{threading.current_thread().name},
Received message value: {message.value}"""
)
# 处理逻辑
start_time
=
time
.
time
()
funasr_service
.
handle_process
(
str
(
message
.
value
.
decode
(
'utf-8'
)))
...
...
@@ -74,7 +125,7 @@ def consume_kafka():
log
.
info
(
"handle_process耗时:"
+
str
(
end_time
-
start_time
))
except
Exception
as
e
:
traceback
.
print_exc
()
log
.
error
(
"
oc
r consumer Exception: "
+
str
(
e
))
log
.
error
(
"
funas
r consumer Exception: "
+
str
(
e
))
# 循环消费消息
# for message in consumer:
...
...
main.py
View file @
0570207
...
...
@@ -219,12 +219,7 @@ def start_process():
if
__name__
==
"__main__"
:
log
.
info
(
"funasr main starting..."
)
# start_kafka()
start_process
()
start_process
()
# process = multiprocessing.Process(target=funasr_service.deal_worker, args=(url, task_id,))
# log.info(f"process:{process}")
# process.start()
funasr_consumer
.
multi_thread_consumer
()
funasr_producer
.
send_wait_task
()
save_path
=
"./audio/"
if
os
.
path
.
exists
(
save_path
):
...
...
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