python多线程

http://python.jobbole.com/86822/

http://www.cnblogs.com/hanybblog/p/6225797.html
两个线程,第一个立即执行完成,输出了'all end',第二个sleep之后,最后也输出'all end'

'''
import os
import datetime
import threading
import pymongo
from time import sleep

#----------------------------------------------------------------------
def t1(name1):
    """"""
    for i in xrange(5):
        print i
    print name1 + '   ' + str(datetime.datetime.now())

#----------------------------------------------------------------------
def t2(name2):
    """"""
    for i in xrange(6,10):
        print i
        sleep(3)
        
    print name2 + '   ' + str(datetime.datetime.now())

threads = []

th1 = threading.Thread(target=t1, args=('This is t1',))
threads.append(th1)

th2 = threading.Thread(target=t2, args=('This is t2',))
threads.append(th2)

for i in threads:
    i.setDaemon(True)
    i.start()
for i in threads:
    i.join()
    print 'all end'
python多线程_第1张图片
image.png

你可能感兴趣的:(python多线程)