无法解码任何JSON对象。线程化Python
我一直在尝试使用线程生成多个HTTP请求无法解码任何JSON对象。线程化Python,python,json,multithreading,oop,python-multithreading,Python,Json,Multithreading,Oop,Python Multithreading,我一直在尝试使用线程生成多个HTTP请求 import sys sys.path.append(r'C:/dict/path') import apiModule import threading token = 'xxxx' apiModule = apiModule.Module(token) urls = [{'url': 'http://www.example.com'}, {'url': 'http://www.example.com/2'}] data = [] for el
import sys
sys.path.append(r'C:/dict/path')
import apiModule
import threading
token = 'xxxx'
apiModule = apiModule.Module(token)
urls = [{'url': 'http://www.example.com'}, {'url': 'http://www.example.com/2'}]
data = []
for element in urls:
thread = threading.Thread(target=apiModule.method(),kwargs=element)
thread.start()
data.append(thread)
首先,我有一个调用API的方法,该方法将dict作为参数并返回一个JSON对象。此外,当我在没有线程的情况下运行它时,它工作得非常好。但是,下面是我在尝试使用线程时遇到的代码和错误
import sys
sys.path.append(r'C:/dict/path')
import apiModule
import threading
token = 'xxxx'
apiModule = apiModule.Module(token)
urls = [{'url': 'http://www.example.com'}, {'url': 'http://www.example.com/2'}]
data = []
for element in urls:
thread = threading.Thread(target=apiModule.method(),kwargs=element)
thread.start()
data.append(thread)
回溯(最近一次呼叫最后一次):
文件“”,第2行,在
thread=threading.thread(target=apimule.method(),kwargs=element)
文件“C:/dict/path”,第54行,在方法中
返回json.load(r.content)
文件“C:\Python27\lib\json\\ uuuuu init\uuuuuu.py”,第326行,加载
返回\u默认\u解码器。解码
文件“C:\Python27\lib\json\decoder.py”,第366行,在decode中
obj,end=self.raw\u decode(s,idx=\u w(s,0.end())
原始解码中的文件“C:\Python27\lib\json\decoder.py”,第384行
raise VALUERROR(“无法解码JSON对象”)
ValueError:无法解码任何JSON对象
任何帮助都将不胜感激 对@falsetru的回复进行扩展:根据:
target
是run()
方法调用的可调用对象
在您的示例中,传递
target=apiModule.method()
,立即调用apiModule
上的方法“method
”。这会引发您看到的错误。当前代码甚至没有到达thread.start()
调用。尝试thread=threading.thread(target=apimule.method,kwargs=element)
。现在我得到了以下错误:“”thread-7中的异常:回溯(最近一次调用):文件“C:\Python27\lib\threading.py”,第554行,在_bootstrap_internal self.run()文件“C:\Python27\lib\threading.py”的第507行中,在run self