import threading
import urllib2
import time
import webapp2
import main
start = time.time()
url = "http://exmple.com?phone="
class BatchSuscriber(webapp2.RequestHandler):
def get(self):
template = main.JINJA_ENVIRONMENT.get_template('batch.html')
self.response.out.write(template.render())
def post(self):
address = self.request.get('address')
numbers = str(self.request.get('numbers')).split(',')
threads = [threading.Thread(target=self.fetch_url, args=(phone,)) for phone in numbers]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
self.response.write("Elapsed Time: %s" % (time.time() - start))
self.response.write("<br>")
def fetch_url(self,phone):
urlHandler = urllib2.urlopen(url+phone)
html = urlHandler.read()
self.response.write(html)
self.response.write("<br>")
self.response.write("'%s\' fetched in %ss" % (url+phone, (time.time() - start)))
self.response.write("<br>")
尝试使用上面的代码异步进行urlfetch。从我的日志来看,似乎呼叫实际上是串行而不是并行。我怎样才能在gae中实现这一目标。感谢。
答案 0 :(得分:2)
尝试使用线程完全是错误的方法。 GAE已在google.appengine.api.urlfetch
中包含asynchronous requests service;你应该使用它。