Python多线程随机冻结(?)

时间:2016-10-01 16:48:57

标签: python multithreading freeze

我正在编写一个脚本,该脚本应该从非常大量的网站上获取一些图片。第一次使用多线程,认为这是必需的,所以得到一个好的运行时。所以问题:脚本运行,但在看似随机数量的传递网站之后,它就不再继续了。它并没有完全冻结,时间刚好从1/100秒的时间上升。到几分钟左右,有时候这么长时间才会关闭它。此外,它似乎并不像某些网站特别负责,有时会得到970,只有200个条目。这里是代码的相关部分:

concurrent = 200
q = Queue(concurrent * 2)

def main(listPath, usagePath, idListPath):
    [...]
    for i in range(concurrent):
        t = Thread(target=work)
        t.daemon = True
        t.start()
    try: 
        for code in usedIDs:
            q.put(code)
        q.join()

    except KeyboardInterrupt:
        sys.exit(1)


def work():
    while True:
        code = q.get()
        picture = getPicture(code)
        if picture is None:
            pass  # todo: find other source or default
        if not code in usage.keys():
            usage[code] = list()
        usage[code].append(picture)
        q.task_done()

希望我能得到所有重要的代码。提前致谢!

1 个答案:

答案 0 :(得分:0)

我的坏人,问题实际上是在getPicture函数中。不管怎样,谢谢你的答案!

相关问题