python 线程、协程简单使用
来源:互联网 发布:c语言新建文件步骤 编辑:程序博客网 时间:2024/06/06 00:38
python简单线程并发示例:
import threadingimport timemutex = threading.Lock()def worker(num): time.sleep(1) mutex.acquire() print "this is ",num mutex.release()if __name__ == '__main__': for i in range(10): th = threading.Thread(target=worker, args=(i,), name="thread %d" % i) th.start()
python 线程池简单使用:
import timeimport threadpooldef worker(num): time.sleep(1) return "this is %d" % numdef callback(request, result): print request.requestID, resultif __name__ == '__main__': pool = threadpool.ThreadPool(10) for i in range(10): reqlist = threadpool.makeRequests(worker, (i,), callback) for req in reqlist: pool.putRequest(req) pool.wait()
简单协程应用:
from greenlet import greenletdef func1(): print "func1", 1 gr2.switch() print "func1", 2 gr2.switch()def func2(): print "func2", 1 gr1.switch() print "func2", 2 gr1.switch()if __name__ == '__main__': gr1 = greenlet(func1) gr2 = greenlet(func2) gr1.switch()
gevent
import geventdef func1(): print "func1", 1 gevent.sleep(0) # check out into func2 print "func1", 2def func2(): print "func2", 1 gevent.sleep(0) print "func2", 2if __name__ == '__main__': gevent.joinall([ gevent.spawn(func1), gevent.spawn(func2), ])
参考链接:http://www.cnblogs.com/suoning/p/5599030.html
阅读全文
0 0
- python 线程、协程简单使用
- Python多线程线程池简单使用
- Python简单线程同步
- 线程简单使用
- 简单使用线程池
- Thread线程简单使用
- 线程的简单使用
- 简单使用线程池
- MFC简单线程使用
- python 简单线程池实例
- Python线程池简单实现
- python之多线程简单实例
- Python线程使用
- Python线程池使用
- python线程的使用
- Python 线程的使用
- Python 线程与协程
- python 日志简单使用
- Android PopupWindow初步使用
- socket编程(二)
- jQuery -- touch事件之滑动判断(左右上下方向)
- 写作建议
- Android (BroadcastReceiver基础)
- python 线程、协程简单使用
- Service使用技巧
- HDU 4798 Skycity【计算机几何】【阅读题】
- MQTT,RabbitMQ初步使用
- 【Sicily】1002. 等价二叉树
- CPaintDC 、CWindowDC、 CClientDC、 CDC的区别与联系
- 递归
- 面试题45:圆圈中最后剩下的数字
- bzoj 3782 上学路线