python 多线程之thread.start_new_thread

python支持跨平台的多线程

创建线程的方式之一就是最基本的 thread.start_new_thread(handler,(params...))

这里给出一个在windows xp 下运行没有问题的例子(虽然到处都是):

# python 2.7
import  time
import  thread
def  timer(i,interval):
    
while  True:
        
# print 'thread timer: %d time %s'%(i,time.ctime())
        ts  =   ' thread:  ' +  str(i) + "  -  " + str(interval)
        
print  ts        
        time.sleep(interval)
def  test(times):
    
for  i  in  range(times):
        
print  i         
        thread.start_new_thread(timer,(i,(i
* 2   +   1 )))
        
if   __name__   ==   ' __main__ ' :
    
print   ' thread test a '
    test(
5 )
    time.sleep(
20 )
    
print   ' main thread exit...\n '

 这里要说明一点

   如果你不加

    time.sleep(20) 这行代码的话, 以上程序会报错(或者线程无法执行):

      

     Unhandled exception in thread started by
    sys.excepthook is missing
    lost sys.stderr

    因为线程管理程序找不到 timer方法(函数)的引用

    我在网上看到类似教程或文章的时候 很多是没有这一行的,不知道用意何在?

 

转载于:https://www.cnblogs.com/vilyLei/articles/2087837.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值