执行多个任务的Python
我的API中有一个端点,它实际上从不同的数据源获取数据,我试图做的是一次向所有数据源发送请求,一旦从一次数据源获得结果,就将结果返回给用户(如果可能,终止所有剩余的请求) python中哪些好的库可以使用? 任何例子都会大有帮助 谢谢您可以使用此库:执行多个任务的Python,python,multithreading,process,parallel-processing,multiprocessing,Python,Multithreading,Process,Parallel Processing,Multiprocessing,我的API中有一个端点,它实际上从不同的数据源获取数据,我试图做的是一次向所有数据源发送请求,一旦从一次数据源获得结果,就将结果返回给用户(如果可能,终止所有剩余的请求) python中哪些好的库可以使用? 任何例子都会大有帮助 谢谢您可以使用此库: from multiprocessing import Process, Queue import time q = Queue() def some_func1(arg1, arg2, q): #this one will take l
from multiprocessing import Process, Queue
import time
q = Queue()
def some_func1(arg1, arg2, q):
#this one will take longer, so we'll kill it after the other finishes
time.sleep(20)
q.put('some_func1 finished!')
def some_func2(arg1, arg2, q):
q.put('some_func2 finished!')
proc1 = Process(target=some_func1,
args = ('arga', 'argb', q))
proc2 = Process(target=some_func2,
args = ('arg1', 'arg2', q))
proc1.start()
proc2.start()
#this will be the result from the first thread that finishes.
#At this point you can wait for the other threads or kill them, or whatever you want.
result = q.get()
print result
#if you want to kill all the procs now:
proc1.terminate()
proc2.terminate()
编辑:在多处理中使用队列,因为它是进程安全的。这里的问题在哪里?@Michal只是想知道我应该使用什么python库来实现这一点,我发现多处理无法真正理解如何调用多个函数并获得响应,或者只是等待第一个响应。。。希望这能给你一个想法,我正在努力实现的目标。ThanksHow那么你能确保第一个回应取消其他回应吗?@AustinMullins见上文。@Eli这正是我想要做的。。。谢谢