【问题标题】:python watchdog for threads用于线程的python看门狗
【发布时间】:2012-12-07 12:49:36
【问题描述】:

我正在编写一个简单的应用程序,它从文件中读取(大约一百万)行,将这些行复制到列表中,如果下一行与上一行不同,它将运行一个线程,以对该列表执行一些工作。线程作业基于 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

【问题讨论】:

  • 看起来很乱。我责怪线程
  • 在任何地方粘贴 logging.debug("where I am now"),看看结果如何。这是我了解“幕后”线程正在发生的事情的唯一方法。
  • 听起来很像packages.python.org/Pyro4 ? (甚至可能是兔子和芹菜)

标签: python multithreading


【解决方案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()

【讨论】:

    猜你喜欢
    • 2011-12-04
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 2020-06-23
    • 2012-04-09
    • 1970-01-01
    相关资源
    最近更新 更多