异步http请求的python线程问题
我正在用urllib2构建一个异步HTTP请求类。代码如下所示:异步http请求的python线程问题,python,multithreading,asynchronous,urllib2,Python,Multithreading,Asynchronous,Urllib2,我正在用urllib2构建一个异步HTTP请求类。代码如下所示: import urllib2, threading, datetime class ExportHandler(urllib2.HTTPHandler): def http_response(self, link, actualdate, nowdate ): link += "&export=csv" export = urllib
import urllib2, threading, datetime
class ExportHandler(urllib2.HTTPHandler):
def http_response(self, link, actualdate, nowdate ):
link += "&export=csv"
export = urllib2.urlopen( link )
for link, actualdate in commissionSummaryLinks:
o = urllib2.build_opener(ExportHandler())
t = threading.Thread(target=o.open, args=(link, actualdate, datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
t.start()
print "I'm asynchronous!"
t.join()
print "ending threading"
Exception in thread Thread-9:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 808, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 761, in run
self.__target(*self.__args, **self.__kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 402, in open
req = meth(req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1123, in do_request_
'Content-length', '%d' % len(data))
TypeError: object of type 'datetime.date' has no len()
只需说明commissionSummaryLinks
已填充,actualdate
是一个date-time.datetime.strtime()对象
无论如何,我从所有已发布的线程收到一个错误,如下所示:
import urllib2, threading, datetime
class ExportHandler(urllib2.HTTPHandler):
def http_response(self, link, actualdate, nowdate ):
link += "&export=csv"
export = urllib2.urlopen( link )
for link, actualdate in commissionSummaryLinks:
o = urllib2.build_opener(ExportHandler())
t = threading.Thread(target=o.open, args=(link, actualdate, datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
t.start()
print "I'm asynchronous!"
t.join()
print "ending threading"
Exception in thread Thread-9:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 808, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 761, in run
self.__target(*self.__args, **self.__kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 402, in open
req = meth(req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1123, in do_request_
'Content-length', '%d' % len(data))
TypeError: object of type 'datetime.date' has no len()
我在OSX上运行这个(如果有关系的话)。有人能告诉我这里的问题是什么吗?当你实例化你的线程时,你需要提供o.open
的参数,而不是ExportHandler
的http\u response
方法的参数
在这种情况下,o.open
具有以下方法签名:
open(self, fullurl, data=None, timeout=<object object>) method of urllib2.OpenerDirector instance
urllib2.OpenerDirectory实例的open(self,fullurl,data=None,timeout=)方法
我猜您只需要设置args=(link,)
如果仍然需要使用这些其他参数,则可能需要修改ExportHandler
的构造函数以获取所需的其他参数,然后在http\u response
方法中酌情使用它们。有关定义类构造函数的更多信息,请参阅。好的,你说得对,这似乎很有效。但是,如果我确实需要在ExportHandler类中执行更多操作的其他参数,该怎么办?“我该如何传递这些信息?”SadMirrow,我扩展了我的原始答案来解决这个问题。太棒了,但我该如何让打印工作正常进行呢?函数没有做它需要做的事情,但是我得到了输出我是异步的
和结束线程
但是在http_repsonse
方法中不会打印任何内容,以便我进行故障排除并查看脚本的执行情况…@sadmove,在http_response
方法中没有任何print
语句,因此,除非在那里添加了内容,否则我不希望打印任何内容。是的,我在代码中添加了一些与OP不同的行。包括一个文件写入语句,该语句应该将文件写入目录,但在成功完成异步请求后,文件不在那里,这让我相信http\U响应()根本就没有人打电话。。。