0

我正在编写简单的应用程序,它从文件中读取(大约一百万)行,将这些行复制到列表中,如果下一行与上一行不同,则它运行一个线程,以对该列表执行一些工作。线程作业基于 tcp 套接字,通过 telnet lib 发送和接收命令。

有时我的应用程序挂起并且什么也不做。我包装到 try-except 语句中的所有 telnet 操作,以及对套接字的读取和写入都有超时。

我考虑过编写看门狗,它会在挂起条件下执行 sys.exit() 或类似的操作。但是,现在我正在考虑如何创建它,但仍然不知道如何去做。所以如果你能追踪到我,那就太好了。

对于该文件,我正在创建 40 个线程。伪代码看起来:

lock = threading.Lock()
no_of_jobs = 0

class DoJob(threading.Thread):
    def start(self, cond, work):
        self.work = work
        threading.Thread.start(self)
    def run(self)
        global lock
        global no_of_jobs
        lock.acquire()
        no_of_jobs += 1
        lock.release()

        # do some job, if error or if finished, decrement no_of_jobs under lock
        (...)
main:
#starting conditions:
with open(sys.argv[1]) as targetsfile:
    head = [targetsfile.next() for x in xrange(1)]
    s = head[0]

    prev_cond = s[0]
    work = []

for line in open(sys.argv[1], "r"):
    cond = line([0])
    if prev_cond != cond:
       while(no_of_jobs>= MAX_THREADS):
           time.sleep(1)

       DoJob(cond, work)
       prev_cond = cond
       work = None
       work = []
     work.append(line)

#last job:
       DoJob(cond, work)

while threading.activeCount() > 1:
    time.sleep(1)

最好的问候J

4

1 回答 1

1

我过去曾成功使用过如下代码(来自我编写的 python 3 程序):

import threading

def die():
    print('ran for too long. quitting.')
    for thread in threading.enumerate():
            if thread.isAlive():
                    try:
                            thread._stop()
                    except:
                            pass
    sys.exit(1)


if __name__ == '__main__':
    #bunch of app-specific code...

    # setup max runtime
    die = threading.Timer(2.0, die) #quit after 2 seconds
    die.daemon = True
    die.start()

    #after work is done
    die.cancel()
于 2012-12-07T13:01:28.843 回答