我用rq解决了这个问题。
python的一个简单消息队列。
首先,需要异步运行的方法的实现。在
它将运行您的nn函数,在本例中,
然后,为消息队列设置一个简单的配置,
我使用redis包中的connectionPool。在
基本上,整个任务都是由rq并行执行的。在def nn(frame):
# some heavy processing code
pass
def asynch_call(frame):
p = Pool(4)
results = p.map(nn, frame)
p.close()
p.join()
pool = redis.ConnectionPool(
host=HOST,
port=PORT,
password=PASS,
db=0)
r = redis.Redis(connection_pool=pool)
q = Queue('nn_queue', connection=r)
count = 0
stack = []
while True:
frame = cv2.imread(0)
detected = detect(frame)
# stop collecting images when collected 5
if detected and count <= 5:
stack.append(frame)
count += 1
# start processing
if len(stack) == 5:
job = q.enqueue(asynch_call, frame, timeout=any_long_timeout )
if job.status=='queued':
print("Job submission ok")
# reset
stack = []
count = 0
为了启动一个处理异步调用的worker,您有几个选项,为Worker创建自己的代码,或者在单独的终端中运行以下命令:
^{pr2}$
请参阅上面用于发送作业的队列名称的命令。
我希望有帮助。在