如何在Python中并行启动2个请求

问题描述 投票:0回答:1

我想在 Python 中执行 2 个在后台执行请求的函数。我不介意等到两者完成,但它们应该并行执行。

我尝试使用

concurrent.futures
库,但这需要我使用异步等等,直到最重要的函数,它是同步的,需要大量重构才能使其异步。

我正在尝试这种方法,但我不确定这是否真的可以正确并行化所有内容

def worker(function, queue_prepared, *args):
    result = function(*args)
    queue_prepared.put(result)

def launch_threads(param1, param2):
    queue_first = queue.Queue()
    queue_second = queue.Queue()
    thread_first = threading.Thread(target=worker, args=(request1, queue_first, param1, param2))
    thread_second = threading.Thread(target=worker, args=(request2, queue_second, param1))
    thread_first.start()
    thread_second.start()
    thread_first.join()
    thread_second.join()
    return queue_first, queue_second


queue_first, queue_second = launch_threads(param1,param2)

queue_first_finished = False
queue_second_finished = False

while not queue_first_finished or not queue_second_finished:
    if not queue_first_finished:
       if not queue_first.empty():
            first = queue_first.get(timeout=1)
       else:
             queue_first_finished = True
     if not queue_second_finished:
       if not queue_second.empty():
            first = queue_second.get(timeout=1)
       else:
             queue_second_finished = True

    
python multithreading parallel-processing
1个回答
0
投票

您不需要使用队列重新发明自己的线程池,而是使用 concurrent.futures.ThreadPoolExecutorconcurrent.futures.as_completed 在任务完成后立即获取任务

import concurrent.futures
import time

def task(x):
    time.sleep(x)
    return x

pool = concurrent.futures.ThreadPoolExecutor(2)

wait_times = [2,1]
tasks = [pool.submit(task,i) for i in wait_times]

for future in concurrent.futures.as_completed(tasks):
    print(future.result())
1
2
© www.soinside.com 2019 - 2024. All rights reserved.