Python concurrent.futures:在多个ThreadPoolExecutor之间共享相同的队列?

时间:2018-03-06 11:59:57

标签: python concurrency parallel-processing

两个ThreadPoolExecutor可以共享同一个队列吗?

在这个例子中,我有两个处理元素的函数略有不同。第一个需要4个线程,第二个只需要2.如何实现第二个函数do_some_stuff_2()以便它处理队列中的其余元素?

我对并发和并行编程很陌生,对不起,如果看起来很蠢。

# elements to process
things_to_process = [element1, element2 ...]

# functions that will process the elements
def do_some_stuff_1(x):
    """
    I need 4 threads, please
    """
    'do some stuff'

def do_some_stuff_2(x):
    """
    I need only 2 threads, please
    """
    'do some different stuff'


with concurrent.futures.ThreadPoolExecutor(max_workers=6) as executor:
    future_to_element = {executor.submit(do_some_stuff, element): element for element in things_to_process}
    for future in concurrent.futures.as_completed(future_to_element):
        try:
            data = future.result()
        except Exception as exc:
            print('{} generated an exception: {}'.format(element, exc))

0 个答案:

没有答案