Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用多处理在Python中运行多个异步进程?_Python_Python 3.x_Asynchronous_Multiprocessing_Python Multiprocessing - Fatal编程技术网

如何使用多处理在Python中运行多个异步进程?

如何使用多处理在Python中运行多个异步进程?,python,python-3.x,asynchronous,multiprocessing,python-multiprocessing,Python,Python 3.x,Asynchronous,Multiprocessing,Python Multiprocessing,我需要运行多个后台异步函数,使用多处理。我有可行的Popen解决方案,但它看起来有点不自然。例如: from time import sleep from multiprocessing import Process, Value import subprocess def worker_email(keyword): subprocess.Popen(["python", "mongoworker.py", str(keyword)]) return True keywor

我需要运行多个后台异步函数,使用多处理。我有可行的Popen解决方案,但它看起来有点不自然。例如:

from time import sleep
from multiprocessing import Process, Value
import subprocess

def worker_email(keyword):
    subprocess.Popen(["python", "mongoworker.py", str(keyword)])
    return True

keywords_list = ['apple', 'banana', 'orange', 'strawberry']

if __name__ == '__main__':
    for keyword in keywords_list:
        # Do work
        p = Process(target=worker_email, args=(keyword,))
        p.start()
        p.join()
如果我尝试不使用Popen,例如:

def worker_email(keyword):
    print('Before:' + keyword)
    sleep(10)
    print('After:' + keyword)
    return True
函数逐个运行,没有异步。那么,如何在不使用Popen的情况下同时运行所有函数呢

UPD:我正在使用multiprocessing.Value从进程返回结果,如:

def worker_email(keyword, func_result):
    sleep(10)
    print('Yo:' + keyword)
    func_result.value = 1
    return True

func_result = Value('i', 0)
p = Process(target=worker_email, args=(doc['check_id'],func_result))
p.start()
# Change status
if func_result.value == 1:
    stream.update_one({'_id': doc['_id']}, {"$set": {"status": True}}, upsert=False)

但是如果没有.join(),它将无法工作。有没有办法让它工作或类似的方式?:)

如果只是删除行
p.join()
它应该可以工作。
如果要在进一步执行之前等待进程完成,只需
p.join
。在程序结束时,python会等待所有进程结束后再关闭,所以您不必担心这一点。

通过将结果检查和状态更新传输到worker函数中,解决了获取进程结果的问题。比如:

# Update task status if work is done
def update_status(task_id, func_result):
    # Connect to DB
    client = MongoClient('mongodb://localhost:27017/')
    db = client.admetric
    stream = db.stream

    # Update task status if OK
    if func_result:
        stream.update_one({'_id': task_id}, {"$set": {"status": True}}, upsert=False)

    # Close DB connection
    client.close()

# Do work
def yo_func(keyword):
    sleep(10)
    print('Yo:' + keyword)
    return True

# Worker function
def worker_email(keyword, task_id):
    update_status(task_id, yo_func(keyword))

你能再解释一下吗?:)我是否需要使用p.close()来代替,就像池一样?@sortas我编辑了答案。不,您不需要
p.close()
。只有池有这个函数。还有一个细节:我正在尝试使用共享值从进程返回结果。类似于:
func_result=Value('i',0)p=Process(target=worker_email,args=(doc['check_id',func_result])p.start()如果func\u result.value==1:stream.update\u one({u id:doc['u id']),{$set:{“status”:True},upsert=False)
,但我现在不能不连接:)有什么想法吗,不知道注释的格式,抱歉:)已添加到UPD。希望你能给我一些建议:)