clo*_*d36 10 python error-handling multithreading python-module python-multithreading
我有一个调用API的脚本.为了加快脚本速度,我试图实现线程化.
当我在IDLE时,下面的脚本工作,但是当我尝试从命令行使用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.
Run Code Online (Sandbox Code Playgroud)
错误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):
Run Code Online (Sandbox Code Playgroud)
我找不到任何关于这些错误的信息.所以,任何帮助表示赞赏.下面是处理线程的脚本部分.
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)
Run Code Online (Sandbox Code Playgroud)
问题是,当您将其作为独立脚本运行时,doWork 中有很多守护线程,但是当只剩下守护线程时脚本就会退出,因此它们都会被解释器退出而杀死。当您在 IDLE 中交互运行它时,解释器不会退出,因此您不会遇到该问题。