我在廖雪峰的博客,

中看到一句话,启动一个线程就是把一个函数传入并创建Thread实例,然后调用start()开始执行:

这是简单粗暴的一句话。

1.导入threading模块,

2.新建一个函数(在这处理其他的事情)

3.启动线程(把一个函数传入并创建Thread实例,然后调用start()开始执行)

 

# 1. 导入threading
import time,threading
# 2.定义一个函数
def loop():
    print 'threading %s running >>>>' % threading.current_thread().name
    n=0
    while n<5:
        print 'threading %s >>>>  %s' % (threading.current_thread().name,n)
        n=n+1
        time.sleep(1)

    print 'threading end %s' % threading.current_thread().name
#
print 'start threading is %s ' % threading.current_thread().name
# 3.启动线程
t=threading.Thread(target=loop,name='childthreading')
t.start()
t.join()

print 'threading end %s' % threading.current_thread().name

 

posted on 2015-08-27 20:43  ทดสอบ  阅读(100)  评论(0)    收藏  举报