这是我的主要功能。如果我收到新的报价,我需要检查付款。我有HandleNewOffer()函数。但是,如果同时有2个(或更多)要约,则会发生此代码的问题。买方之一将不得不等到交易结束。那么这是否有可能使用HandleNewOffer()函数生成新流程并在同时完成多个事务时将其杀死呢?先感谢您。
def handler():
try:
conn = k.call('GET', '/api/').json() #connect
response = conn.call('GET', '/api/notifications/').json()
notifications = response['data']
for notification in notifications:
if notification['contact']:
HandleNewOffer(notification) # need to dynamically start new process if notification
except Exception as err:
error= ('Error')
Send(error)
答案 0 :(得分:0)
我建议在此处使用“工人池”模式将并发调用的数量限制为HandleNewOffer
。
concurrent.futures
模块提供了上述模式的现成实现。
from concurrent.futures import ProcessPoolExecutor
def handler():
with ProcessPoolExecutor() as pool:
try:
conn = k.call('GET', '/api/').json() #connect
response = conn.call('GET', '/api/notifications/').json()
# collect notifications to process into a list
notifications = [n for n in response['data'] if n['contact']]
# send the list of notifications to the concurrent workers
results = pool.map(HandleNewOffer, notifications)
# iterate over the list of results from every HandleNewOffer call
for result in results:
print(result)
except Exception as err:
error= ('Error')
Send(error)
此逻辑将并行处理与计算机拥有的CPU内核数量一样多的商品。