python - 在 python 中稍后杀死线程

标签 python multithreading thread-safety subprocess

我有一个带有线程的Python代码,我需要如果在例如1小时内线程未完成,则完成所有线程并完成脚本,如果一小时未完成,则等待我的所有线程完成。

我尝试使用守护线程,并进行一小时的 sleep ,如果该小时已完成,则使用: sys.exit() 但它对我不起作用,因为总是等待我的 sleep 线程,然后我的脚本等待线程完成并且 sys.exit() 不起作用。

import socket, threading, time, sys
from sys import argv
import os

acc_time=0
transactions_ps=5

ins = open(sys.argv[1],'r')
msisdn_list = []
for line in ins:
    msisdn_list.append (line.strip('\n'))
   # print line
ins.close()


def worker(msisdn_list):
    semaphore.acquire()
    global transactions_ps
    print "  *****  ", threading.currentThread().getName(), "Lanzado"
    count=1
    acc_time=0
    print "len: ",len(msisdn_list)
    for i in msisdn_list:
        try:
            init=time.time()
            time.sleep(2)
            print "sleeping...",i
            time.sleep(4)
            final=time.time()
            acc_time = acc_time+final-init
            print acc_time
        except IOError:
                print "Connection failed",sys.exc_info()[0]

    print "Deteniendo ",threading.currentThread().getName()
    semaphore.release()
def kill_process(secs_to_die):
    time.sleep(secs_to_die)
    sys.exit()

seconds_to_die=3600

thread_kill = threading.Thread(target = kill_process, args=(seconds_to_die,))
thread_kill.start()

max_con=5
semaphore = threading.BoundedSemaphore(max_con)
for i in range(0,28,transactions_ps):
    w = threading.Thread(target=worker, args=(msisdn_list[i:i+transactions_ps-1],))
    w.setDaemon(True)
    w.start()

如何做到

最佳答案

对代码进行的最小更改可以解决该问题:threading.Barrier:

barrier = Barrier(number_of_threads, timeout=3600)
# create (number_of_threads - 1) threads, pass them barrier
# each thread calls barrier.wait() on exit
barrier.wait() # after number_of_threads .wait() calls or on timeout it returns

更简单的替代方法是使用 multiprocessing.dummy.Pool 创建守护线程:

from multiprocessing.dummy import Pool # use threads

start = timer()
endtime = start + 3600
for result in pool.imap_unordered(work, args):
    if timer() > endtime:
       exit("timeout") 

在工作项完成之前,代码不会超时,即,它预计处理列表中的单个项目不会花费很长时间。

完整示例:

#!/usr/bin/env python3
import logging
import multiprocessing as mp
from multiprocessing.dummy import Pool
from time import monotonic as timer, sleep

info = mp.get_logger().info

def work(i):
    info("start %d", i)
    sleep(1)
    info("end %d", i)

seconds_to_die = 3600
max_con = 5
mp.log_to_stderr().setLevel(logging.INFO) # enable logging
pool = Pool(max_con) # no more than max_con at a time
start = timer()
endtime = start + seconds_to_die
for _ in pool.imap_unordered(work, range(10000)):
    if timer() > endtime:
        exit("timeout")

关于python - 在 python 中稍后杀死线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25582794/

相关文章:

python - 用 Graphviz 显示这个决策树

python - AttributeError: 'Series' 对象没有属性 'days'

python - 如何在 python 中解析 json 数据时解析多索引值并创建 csv 文件

python - Python如何一次上传多个文件到云文件?

scala - 线程安全地更新 Scala 集合

c - openSSL 读/写线程安全

python - 在python-igraph中,查找两个顶点之间的边的数量和众数

java - 程序顺序规则在构造函数中起作用之前是否发生?

java - 高效执行独占执行

multithreading - 两个并发请求,相同的 JPA 实体 - 有问题吗?