打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
进程池和线程池、协程
import threadpool,threading
import time,os
def sayhello (a):
print("%shello:%s "%(threading.Thread(),a))
time.sleep(2)



if __name__ == '__main__':
global result
seed = ["a"]
task_pool=threadpool.ThreadPool(2)#创建线程池
#p=Process()创建进程池
#p.apply(func,args.kwargs) 执行进程
#p.close() p.jion()
requests = threadpool.makeRequests(sayhello,seed)#创建任务 makeRequests(func,args,kwargs)
for request in requests:
task_pool.putRequest(request)#把任务put到线程池中

task_pool.wait()#执行等待线程池中的线程执行完毕

------------------------------------------------------------------------------------------
协程
import gevent,os,requests

def func1(url):
data = requests.get(url)
print(data.text)

gevent.joinall([ #开启轮询
gevent.spawn(func1,"http://www.baidu.com"), #注册函数对象并执行函数
gevent.spawn(func1,"https://github.com/")
])
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
python线程池(threadpool)
Gevent指南
Python并发编程协程(Coroutine)之Gevent
【连载电子书九】Python并发编程
进程、线程和协程的区别是什么
linux下C实现多线程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服