繁体   English   中英

Python并发ThreadPoolExecutor - 如果满足条件则停止执行

[英]Python Concurrency ThreadPoolExecutor - stop execution if condition is met

我坚持尝试使用 ThreadPoolExecutor 进行多线程,如果其中一个进程满足标准,则执行将安全停止,以便不必完成剩余的线程。

我有以下概念,但它不起作用,它一直在运行:

import time, sys
from concurrent.futures import ThreadPoolExecutor

data = {"future1": 'blank', "future2": 'blank', "future3": 'blank'}

def function1(n):
   global data #not sure if this is necessary, as it seems to be able to access this anyway?
   print(n)
   time.sleep(1)
   data['future1'] = n

def function2(n):
   global data
   print(n)
   time.sleep(2)
   data['future2'] = n

def function3(n):
   global data
   print(n)
   time.sleep(3)
   data['future3'] = n

with ThreadPoolExecutor(max_workers=4) as executor:
  while True:
    future1=executor.submit(function1, 'test1')
    future2=executor.submit(function2, 'test2')
    future3=executor.submit(function3, 'test3')

    if data['future2']!='blank':
      executor.shutdown(wait=False)
      sys.exit()

不确定我在这里做错了什么,任何帮助将不胜感激。

这是完整的答案,事实证明executor.shutdown(wait=False)不是 Sachin 提到的方法。 完全归功于https://gist.github.com/clchiou/f2608cbe54403edb0b13

import time, sys
from concurrent.futures import ThreadPoolExecutor
import concurrent.futures.thread

data = {"future1": None, "future2": None, "future3": None}

def function1(n):
   time.sleep(1)
   data['future1'] = n
   print(n)

def function2(n):
   time.sleep(2)
   data['future2'] = n
   print(n)

def function3(n):
   time.sleep(3)
   data['future3'] = n
   print(n)

with ThreadPoolExecutor(max_workers=4) as executor:
  executor.submit(function1, 'test1')
  executor.submit(function2, 'test2')
  executor.submit(function3, 'test3')

  while True:
    if any(v is not None for v in data.values()):
      executor._threads.clear()
      concurrent.futures.thread._threads_queues.clear()
      break

print(data)

您正在while True:循环中运行线程,它一次又一次地启动调用。

with ThreadPoolExecutor(max_workers=4) as executor:

    future1=executor.submit(function1, 'test1')
    future2=executor.submit(function2, 'test2')
    future3=executor.submit(function3, 'test3')

    while True:
        if data['future2']!='blank':
            executor.shutdown(wait=False)
            sys.exit()

仍然executor.shutdown(wait=False)会等待它的子进程完成

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM