繁体   English   中英

如何在python中动态创建新进程?

[英]How can I dynamically create new process in python?

这是我的主要功能。 如果我收到新的报价,我需要检查付款。 我有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)

我建议在这里使用“工人池”模式来限制对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内核一样多的报价。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM