多彩编程 多彩编程MZPH · CODE BLOG
ARTICLE DETAIL

文章详情

深耕前端与后端开发技术的一线实战笔记与踩坑复盘。

【2014-04-02】​python3下的多线程实例

【2014-04-02】​python3下的多线程实例 [历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-04-02 标题​python3下的多线程实例分类编程 / python jython 标签多线程·python jythonpython3下的多线程实例最简单的多线程基于\_thread继承threading.Thread使用threading.Thread构造函数本文的python版本3.3.3python的多线程代码网上到处都是但是基于python3的不太好找这里作者备份下最简单的3种实例。python3的lib【 https://docs.python.org/3.3/library/threading.html#module-threading 】【 https://docs.python.org/3.3/library/_thread.html#module-_thread 】最简单的多线程基于_threadimport_threadimporttimedefhello(index,count):whilecount0:countcount-1;time.sleep(1);print(hello from %d, count %d%(index,count));if__name____main__:_thread.start_new_thread(hello,(1,5));_thread.start_new_thread(hello,(2,8));继承threading.ThreadimportthreadingimporttimeclassHello(threading.Thread):def__init__(self,threadname,count):print(Hello Thread Init);threading.Thread.__init__(self,namethreadname)self.countcount;defrun(self):whileself.count0:self.countself.count-1;time.sleep(1);print(hello from %s, count %d%(self.name,self.count));if__name____main__:thread1Hello(A,5);thread2Hello(B,8);thread1.start();thread2.start();使用threading.Thread构造函数importthreadingimporttimedefhello(index,count):whilecount0:countcount-1;time.sleep(1);print(hello from %d, count %d%(index,count));if__name____main__:thread1threading.Thread(targethello,args(1,5));thread2threading.Thread(targethello,args(2,8));thread1.start();thread2.start();多线程同步及队列暂时先略过。。
返回列表