【发布时间】: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