擅长:python、mysql、java
<p>这是一个尝试<a href="http://www.gevent.org/" rel="nofollow">gevent</a>的好机会。在</p>
<p>你应该为请求.get使应用程序不必等待IO阻塞。在</p>
<p>然后,您可以生成多个worker,并使用队列来传递请求和文章。
也许是类似的事情:</p>
<pre><code>import gevent.monkey
from gevent.queue import Queue
from gevent import sleep
gevent.monkey.patch_all()
MAX_REQUESTS = 10
requests = Queue(MAX_REQUESTS)
articles = Queue()
mock_responses = range(100)
mock_responses.reverse()
def request():
print "worker started"
while True:
print "request %s" % requests.get()
sleep(1)
try:
articles.put('article response %s' % mock_responses.pop())
except IndexError:
articles.put(StopIteration)
break
def run():
print "run"
i = 1
while True:
requests.put(i)
i += 1
if __name__ == '__main__':
for worker in range(MAX_REQUESTS):
gevent.spawn(request)
gevent.spawn(run)
for article in articles:
print "Got article: %s" % article
</code></pre>