- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 ProcessPoolExecutor 来生成子进程。目前,我正在尝试通过键盘中断/Ctrl+C 优雅地退出脚本。
我正在创建包含 2 个工作人员的池并提交 5 个 future。在中断时,我试图取消所有尚未执行的 future 。如果我在前两个 future 执行期间中断,池只能取消两个 future,这意味着当前正在运行三个 future。但我只有两个 worker ,每个进程运行 5 秒。我的 future 正在执行什么或为什么?
import subprocess
from concurrent.futures import ProcessPoolExecutor
import signal
import sys
def executecommands(commands):
# Replace signal handler of parent process, so child processes will ignore terminate signals
original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
processpool = ProcessPoolExecutor(1)
# Restore original handler, so the parent process can handle terminate signals
signal.signal(signal.SIGINT, original_sigint_handler)
futures = []
try:
for command in commands:
futures.append(processpool.submit(executecommand, command))
processpool.shutdown()
print("Executed commands without interruption")
except KeyboardInterrupt:
print("\nAttempting to cancel pending commands..")
for future in futures:
if future.cancel():
print("Cancelled one command")
else:
print("One command was already running, couldn't cancel")
print("Waiting for running processes to finish..")
processpool.shutdown()
print("Shutdown complete")
sys.exit(0)
def executecommand(command):
# create a subprocess and run it
print("running command")
process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print("finished command")
return process
if __name__ == '__main__':
print("lets go")
commandlist = [['/bin/sleep', '5'], ['/bin/sleep', '5'], ['/bin/sleep', '5'], ['/bin/sleep', '5'], ['/bin/sleep', '5']]
executecommands(commandlist)
最佳答案
这是 CPython 实现细节,但您唯一可以取消的 future 是不在“调用队列”中的 future。调用队列包含接下来要执行的所有 future。其大小为 max_workers + EXTRA_QUEUED_CALLS
。 ( EXTRA_QUEUED_CALLS
is currently set to 1 .)
在您的情况下,当前两个 future 开始执行时,调用队列将填充接下来的 3 个 future(max_workers
为 2,EXTRA_QUEUED_CALLS
为 1)。由于您总共只有 5 个 future ,因此您无法取消其中任何一个。
如果您在命令列表中填写了 2 个 worker 的 10 个 future,您将能够取消最后 5 个 future:
lets go
running command
running command
^C
Attempting to cancel pending commands..
One command was already running, couldn't cancel
One command was already running, couldn't cancel
One command was already running, couldn't cancel
One command was already running, couldn't cancel
One command was already running, couldn't cancel
Cancelled one command
Cancelled one command
Cancelled one command
Cancelled one command
Cancelled one command
Waiting for running processes to finish..
running command
running command
finished command
finished command
running command
finished command
Shutdown complete
关于python - ProcessPoolExecutor 锁定了超出必要数量的 future,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35700273/
python ProcessPoolExecutor 在命令行中工作,但添加到函数后不运行 它是这样工作的 from concurrent import futures def multi_proce
如果父进程因任何原因终止,有没有办法使 concurrent.futures.ProcessPoolExecutor 中的进程终止? 一些细节:我在处理大量数据的作业中使用 ProcessPoolEx
这是我关于 stackoverflow 的第一个问题。我基本上能够在这里找到我需要知道的东西。顺便说一句,非常感谢。 但是。如果我尝试终止我的 ProcessPoolExecutor,它只会在生成的整
我有一个代码,它从 gstorage 下载文件,将它们转储到 json,然后将该 json 转为 csv,然后转为 parquet,最后上传到 aws s3(不要问为什么我不是写它的人)。 我从我的日
ESPNP 播放器免费 class ESPNPlayerFree: def __init__(self, player_id, match_id, match_id_team): ... 团队列表1:
在本文档 ( https://pymotw.com/3/concurrent.futures/ ) 中说: “ProcessPoolExecutor 的工作方式与 ThreadPoolExecutor
我正在创建一个多处理程序来处理多个批处理,但我的日志记录无法将批处理记录到日志文件中,只会记录根 log.info,如何设置日志记录以正确打印到日志文件? 日志只会打印这样一行"INFO:root:t
我正在尝试使用一个单独的进程通过并发 future 流式传输数据。然而,另一方面,有时对方会停止数据馈送。但只要我重新启动这个 threadable 然后它就会再次工作。所以我设计了这样的东西,以便能
设置 我设置了一个函数来接收多个关键字参数: def process(image, folder, param1, param2, param3): do_things return
from concurrent.futures import ProcessPoolExecutor import os import time def parInnerLoop(item):
python 3.6.6 这是代码: import asyncio import time from concurrent.futures import ProcessPoolExecutor exe
我是一般并行化的新手,特别是 concurrent.futures。我想对我的脚本进行基准测试并比较使用线程和进程之间的差异,但我发现我什至无法运行它,因为在使用 ProcessPoolExecuto
代码: if __name__ == "__main__": p = ProcessPoolExecutor() p.submit(lambda x: print(x), "somet
代码: if __name__ == "__main__": p = ProcessPoolExecutor() p.submit(lambda x: print(x), "somet
我想在多进程环境中记录到单个文件。我可以得到 sample code从 python 日志记录手册开始工作。但是当我替换为 ProcessPoolExecutor 时,它不起作用。 # work
我正在尝试使用新的 Tornado queue对象以及 concurrent.futures允许我的网络服务器将 CPU 密集型任务传递给其他进程。我想访问从 concurrent.futures 模
我有一个 python 函数正在调用我无法控制或更新的 C 库。不幸的是,C 库存在间歇性错误,有时会挂起。为了防止我的应用程序也挂起,我尝试隔离 ThreadPoolExecutor 或 Proce
我最近开始使用 Python 的多线程和多处理功能。 我尝试编写代码,使用生产者/消费者方法从 JSON 日志文件中读取 block ,将这些 block 作为事件写入队列,然后启动一组将从该队列中轮
我有一大堆必须以某种方式处理的元素。我知道可以通过以下方式使用多处理过程来完成: pr1 = Process(calculation_function, (args, )) pr1.start() p
if __name__ == '__main__': MATCH_ID = str(doc_ref2.id) MATCH_ID_TEAM = doc_ref3.id with
我是一名优秀的程序员,十分优秀!