以下示例显示如何同时运行任务。
import gevent from gevent import socket urls = ['www.baidu.com', 'www.sina.com', 'www.sohu.com'] jobs = [gevent.spawn(socket.gethostbyname, url) for url in urls] gevent.joinall(jobs) [job.value for job in jobs]
['112.80.248.73', '101.71.100.123', '140.207.205.39']
调用函数
# -*-coding=utf-8 -*- from __future__ import print_function import gevent from gevent import monkey monkey.patch_all() import requests urls = ['http://www.baidu.com','http://www.sina.com.cn','http://www.sohu.com','http://www.baidu.com','http://www.sina.com.cn','http://www.sohu.com','http://www.baidu.com','http://www.sina.com.cn','http://www.sohu.com'] #多些URL可以更好的看到并行执行任务 def print_head(url): print ('starting %s'%url) data = requests.get(url).text print ('%s:%s bytes %r'%(url,len(data),data[:50])) jobs = [gevent.spawn(print_head,url) for url in urls] gevent.wait(jobs)
转载于:https://blog.51cto.com/guoshiwei/2096710