11

我有一个调用 API 的脚本。为了加快脚本速度,我尝试实现线程。

下面的脚本在我处于空闲状态时有效,但是当我尝试从命令行使用 sys argv 运行它时,我收到了下面列出的两种类型的错误。

错误 1

Fatal Python error: PyImport_GetModuleDict: no module dictionary!

This application has requests the Runtime to terminate it in an unusual way.  Please         contact the application's support team for more information.

错误 2

Exception in thread Thread-1 (most likely raised during iterpreter shutdown): 
Exception in thread Thread-2 (most likely raised during iterpreter shutdown):
Exception in thread Thread-3 (most likely raised during iterpreter shutdown):
Exception in thread Thread-5 (most likely raised during iterpreter shutdown):

我找不到关于这些错误的任何信息。因此,任何帮助表示赞赏。下面是处理线程的脚本部分。

import threading
import diffbot

urls = [[example.com],[example2.com]]
data = []

def getData(url):
        x = diffbot.classify(url)
    data.append(x)


def doWork(urls):
    for element in urls:
        for url in element:
            t = threading.Thread(target=getData, args=(url,))
            t.daemon = True
            t.start()

doWork(urls)
4

2 回答 2

3

问题是当你作为一个独立的脚本运行它时,你在 doWork 中有很多守护线程,但是当只剩下守护线程时脚本会退出,所以它们都会被解释器退出而杀死。当您在 IDLE 中以交互方式运行它时,解释器不会退出,因此您不会遇到该问题。

于 2015-04-16T02:16:49.907 回答
0

这解释了这个错误 - https://bugs.python.org/issue26153 - 虽然不确定它是否适用于你的情况

于 2020-05-01T07:02:01.143 回答