Threading 多线程
Threading
是Python
自带的模块,无需安装。
args
传递的必须是“元组”,所以单元素时,必须写成(x,)
形式;
#!/usr/bin/env python
# coding: utf-8
import time
import threading
def task1(who):
while True:
print "hello " + str(who)
time.sleep(1)
def task2():
while True:
print "What's up, man?"
time.sleep(2)
t1 = threading.Thread(target=task1, args=('Qige',))
t2 = threading.Thread(target=task2)
t1.start()
t2.start()
#!/usr/bin/env python
from time import sleep
form threading import Thread
def task1(who):
while True:
print "hello %s" % (str(who))
sleep(1)
def task2():
while True:
print "what's up, man?"
sleep(2)
t1 = Thread(target = task1, args = ('Qige',))
t1.start()
t2 = Thread(target = task2)
t2.start()