Simple case of multithreading in python

#coding=utf-8
import threading
from time import ctime,sleep


def music(func):
    for i in range(2):
        print "I was listening to %s. %s" %(func,ctime())
        sleep(4)

def move(func):
    for i in range(2):
        print "I was at the %s! %s" %(func,ctime())
        sleep(5)

threads = []
t1 = threading.Thread(target=music,args=(u'love business',))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u'阿凡达',))
threads.append(t2)

if __name__ == '__main__':
    for t in threads:
        # Declare the thread as a daemon thread, which must be set before the start() method is called. If it is not set as a daemon thread, the program will be suspended infinitely.
        t.setDaemon(True)
        t.start()

    # Wait for the two processes in the for loop to end before executing the main process
    for t in threads:
        t.join()

    print "all over %s" %ctime()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325560265&siteId=291194637