gpt4 book ai didi

python - 如何让多处理 python 应用程序干净地退出

转载 作者:行者123 更新时间:2023-11-28 20:45:26 24 4
gpt4 key购买 nike

当我运行一个使用多处理的 python 脚本时,我发现很难让它在收到 Ctrl-C 时完全停止。必须多次按下 Ctrl-C,屏幕上会出现各种错误消息。

How can you make a python script that uses multiprocessing and quits cleanly when it receives a Ctrl-C ?

以此脚本为例

import numpy as np, time
from multiprocessing import Pool

def countconvolve(N):
np.random.seed() # ensure seed is random

count = 0
iters = 1000000 # 1million

l=12
k=12
l0=l+k-1

for n in range(N):
t = np.random.choice(np.array([-1,1], dtype=np.int8), size=l0 * iters)
v = np.random.choice(np.array([-1,1], dtype=np.int8), size = l * iters)
for i in xrange(iters):
if (not np.convolve(v[(l*i):(l*(i+1))],
t[(l0*i):(l0*(i+1))], 'valid').any()):
count += 1
return count

if __name__ == '__main__':
start = time.clock()

num_processes = 8
N = 13

pool = Pool(processes=num_processes)
res = pool.map(countconvolve, [N] * num_processes)
print res, sum(res)

print (time.clock() - start)

最佳答案

Jon 的解决方案可能更好,但这里使用的是信号处理程序。我在 VBox VM 中尝试过它,它非常慢,但有效。希望对您有所帮助。

import numpy as np, time
from multiprocessing import Pool
import signal

# define pool as global
pool = None


def term_signal_handler(signum, frame):
global pool

print 'CTRL-C pressed'
try:
pool.close()
pool.join()
except AttributeError:
print 'Pool has been already closed'


def countconvolve(N):
np.random.seed() # ensure seed is random

count = 0
iters = 1000000 # 1million

l=12
k=12
l0=l+k-1

for n in range(N):
t = np.random.choice(np.array([-1,1], dtype=np.int8), size=l0 * iters)
v = np.random.choice(np.array([-1,1], dtype=np.int8), size = l * iters)
for i in xrange(iters):
if (not np.convolve(v[(l*i):(l*(i+1))],t[(l0*i):(l0*(i+1))], 'valid').any()):
count += 1
return count


if __name__ == '__main__':
# Register the signal handler
signal.signal(signal.SIGINT, term_signal_handler)

start = time.clock()

num_processes = 8
N = 13

pool = Pool(processes=num_processes)
res = pool.map(countconvolve, [N] * num_processes)
print res, sum(res)

print (time.clock() - start)

关于python - 如何让多处理 python 应用程序干净地退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23540977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com