异步http请求的python线程问题

时间:2014-03-30 23:16:10

标签: 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 = 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"

我只想说commissionSummaryLinks已填充且actualdatedate time.datetime.strptime()对象。

无论如何,我收到所有发布的线程中的错误,如下所示:

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()

我在OS X上运行它(如果重要的话)。谁能告诉我这里的问题是什么?

1 个答案:

答案 0 :(得分:1)

当您实例化线程时,您需要提供o.open的参数,而不是http_response的{​​{1}}方法的参数。

在这种情况下,ExportHandler具有以下方法签名:

o.open

我的猜测是你只需要设置open(self, fullurl, data=None, timeout=<object object>) method of urllib2.OpenerDirector instance

如果您仍然需要使用其他参数,您可能希望修改args=(link,)的构造函数以获取您需要的其他参数,然后在{{1}中适当地使用它们} 方法。有关定义类构造函数的更多信息,请查看the Python Class tutorial