python多线程的基本使用
来源:互联网 发布:linux 禁止ping 编辑:程序博客网 时间:2024/05/16 18:06
python下实现多线程有两种方式:一种是通过函数的方式产生新的线程,另外一种是通过面向对象的方式实现
通过调用thread模块中的start_new_thread()函数来产生新线程
#!/usr/bin/env python#encoding:utf-8#author:zhxiaimport threadimport timethread_count=0;def test(num,interval): for x in xrange(1,10): print 'current thread is:%d,and x is:%d'%(num,x) time.sleep(interval) thread.exit_thread()if __name__=='__main__': thread.start_new_thread(test,(1,1)) thread.start_new_thread(test,(2,1)) thread.start_new_thread(test,(3,1)) #为了防止主线程在子线程之前退出,需要检测当前的子线程数目,直到所有的子线程执行完毕 time.sleep(0.001) #必需sleep一下,否则取不到子线程的数目 while thread._count()>0: time.sleep(0.5)
通过threading模块实现:
#!/usr/bin/env python#encoding:utf-8#author:zhxiaimport timeimport threadingimport sysclass test(threading.Thread): def __init__(self,num,interval): threading.Thread.__init__(self) self.thread_num=num self.interval=interval def run(self): for x in xrange(1,9): print 'current thread is %d,and x is %d'%(self.thread_num,x) time.sleep(self.interval)if __name__=='__main__': t1=test(1,1) t2=test(2,1) t3=test(3,1) t1.start(); t2.start(); t3.start();
0 0
- python多线程的基本使用
- python 多线程的使用
- python多线程的使用
- Win32 多线程的基本使用
- 多线程的基本使用实例
- iOS多线程的基本使用
- 多线程:NSOperation 的基本使用
- 多线程:NSOperation 的基本使用
- Android 多线程的基本使用
- Python的基本使用
- python多线程的简单使用
- python中多线程的使用
- 多线程的使用的基本模型
- Python-memcached的基本使用
- python的socket基本使用
- Python-memcached的基本使用
- python的语法基本使用
- python 基本数据类型的使用
- asp.net Calendar 控件
- matlab-基础 求取字符串中各个字符的ASCII码
- 银行对公风险分析
- jdk-logging、log4j、logback日志介绍及原理一
- time及ctime函数的使用
- python多线程的基本使用
- 线程死锁
- python 生成身份证号码
- adaboost(二)程序
- MapReduce 顺序组合, 迭代式,组合式,链式
- TreeMap与红黑树
- cocoapods安装第三方框架失败.
- 求最小公倍数
- Eclipse部分操作总结