【问题标题】:Are there like "asyncio.gather()" to run multiple threads or processes together in Python?是否有像“asyncio.gather()”这样在 Python 中一起运行多个线程或进程?
【发布时间】:2022-11-11 04:13:26
【问题描述】:

下面有 2 组代码来运行多个线程或多个进程。

多线程:

from threading import Thread
import queue

def test1(num1, num2, q):
    q.put(num1 + num2)

def test2(num1, num2, q):
    q.put(num1 + num2)

queue1 = queue.Queue()
queue2 = queue.Queue()

thread1 = Thread(target=test1, args=(2, 3, queue1))
thread2 = Thread(target=test2, args=(4, 5, queue2))
thread1.start()
thread2.start()
thread1.join()
thread2.join()

print(queue1.get()) # 5
print(queue2.get()) # 9

多进程:

from multiprocessing import Process, Queue

def test1(num1, num2, q):
    q.put(num1 + num2)

def test2(num1, num2, q):
    q.put(num1 + num2)

queue1 = Queue()
queue2 = Queue()

process1 = Process(target=test1, args=(2, 3, queue1))
process2 = Process(target=test2, args=(4, 5, queue2))
process1.start()
process2.start()
process1.join()
process2.join()

print(queue1.get()) # 5
print(queue2.get()) # 9

而且,下面的代码可以同时运行多个async 任务:

import asyncio

async def test1(num1, num2):
    return num1 + num2

async def test2(num1, num2):
    return num1 + num2

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
                                                  # Here
result1, result2 = loop.run_until_complete(asyncio.gather(test1(2, 3), test2(4, 5)))  
print(result1) # 5
print(result2) # 9

现在,我想知道是否有像上面的asyncio.gather()这样的函数来一起运行多个线程或进程,如下所示:

多线程:

from threading import Thread
import queue

def test1(num1, num2):
    q.put(num1 + num2)

def test2(num1, num2):
    q.put(num1 + num2)
                         # Here
result1, result2 = Thread.gather(test1(2, 3), test2(4, 5))
print(result1) # 5
print(result2) # 9

多进程:

from multiprocessing import Process

def test1(num1, num2):
    q.put(num1 + num2)

def test2(num1, num2):
    q.put(num1 + num2)
                          # Here
result1, result2 = Process.gather(test1(2, 3), test2(4, 5))
print(result1) # 5
print(result2) # 9

那么,有没有像asyncio.gather() 这样在 Python 中一起运行多个线程或进程?

【问题讨论】:

  • 不,您当然可以将您的线程/进程存储在一个列表中并执行for t in threadlist: / t.join()。您无需等待join 从队列中获取——您可以在结果生成后立即读取结果,然后再加入。

标签: python python-3.x python-multiprocessing python-multithreading gather


【解决方案1】:

这就是concurrent.futures 模块存在的原因。

import concurrent.futures
import time
import asyncio

def print_hi():
    time.sleep(2)
    print("hi")

if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    threadpool = concurrent.futures.ThreadPoolExecutor()
    processpool = concurrent.futures.ProcessPoolExecutor()
    feature1 = loop.run_in_executor(threadpool, print_hi)
    feature2 = loop.run_in_executor(processpool, print_hi)
    # Here
    result1, result2 = loop.run_until_complete(asyncio.gather(feature1, feature2))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多