Python threading的简单用法-实例说明
1、如何开启一个线程:代码如下
#!/usr/bin/python
#coding = utf-8
import threading
import time
def handle():
for item in["hello","threading","bye-bye"]:
printitem
def run():
t =threading.Thread(target=handle)
t.start()
if __name__ == '__main__':
print"main thread starting..."
run()
print "main thread finished..."
结果为:
[root@localhost test_del]# python testtheading.py
main thread starting...
hello
threading
bye-bye
main thread finished...
但是多运行几次,会出现这样的结果:
[root@localhost test_del]# python testtheading.py
main thread starting...
main thread finished...
hello
threading
bye-bye
为什么会出现不同的结果呢?
注意:在python中,默认情况下主线程运行结束后会一直等待工作线程运行完成,整个程序才算运行结束。这和c/c++中的线程运行是不同的,在c/c++中,当主线程运行完成,会主动杀死所有工作线程。
2、如何设置线程为后台线程—等同于c/c++线程的运行情况
程序如下:
#!/usr/bin/python
#coding = utf-8
import threading
import time
def handle():
for item in["hello","threading","bye-bye"]:
printitem
def run():
t =threading.Thread(target=handle)
t.setDaemon(True)
t.start()
if __name__ == '__main__':
print"main thread starting..."
run()
print"main thread finished..."
多运行几次,会出现如下情况:
[root@localhost test_del]# python testtheading.py
main thread starting...
main thread finished...
这就表明工作线程还没有开始运行就被kill掉了。
如何保证工作线程运行完后,主线程才能结束呢?
3、线程阻塞—threading.join()
程序如下:
#!/usr/bin/python
#coding = utf-8
import threading
import time
def handle():
for item in["hello","threading","bye-bye"]:
printitem
def run():
t =threading.Thread(target=handle)
t.setDaemon(True)
t.start()
t.join()
if __name__ == '__main__':
print"main thread starting..."
run()
print"main thread finished..."
threading.join()语句使得主线程会一直等待工作线程运行,直到工作线程运行完成,才开始继续执行主线程。无论运行多少次,其结果都为:
[root@localhost test_del]# python testtheading.py
main thread starting...
hello
threading
bye-bye
main thread finished...
4、如何给线程加锁和解锁—互斥锁
语法:共三行
Mutex = threading.Lock() --创建锁
Mutex.acquire([timeout]) --加锁,有个可选参数timeout,用于设定加锁时间
Mutex.release() --解锁
程序如下:未使用线程锁程序代码
#!/usr/bin/python
#coding = utf-8
import threading
import time
num = 0
def handle(i):
global num
while True:
time.sleep(2)
num += 1
print"thread %d:" %i,num
def run():
for i inrange(10):
t = threading.Thread(target=handle,args=(i,))
t.start()
if __name__ == '__main__':
print"main thread starting..."
run()
print"main thread finished..."
程序结果如下:
thread 9: 201
thread 8: 202
thread 4: 203
thread 5: 204
thread 1: 205
thread 2: 207
thread 6: 208
thread 3: 208
thread 0: 209
thread 7: 210
分析程序运行结果,会发现出现了多个线程的重复计算,这显然是不对的。
对上面的程序代码进行加锁,如下所示:
#!/usr/bin/python
#coding = utf-8
import threading
import time
num = 0
mutex =threading.Lock()
def handle(i):
global num
global mutex
while True:
time.sleep(2)
if mutex.acquire():
num += 1
print "thread %d:" %i,num
mutex.release()
def run():
for i inrange(10):
t =threading.Thread(target=handle, args=(i,))
t.start()
if __name__ == '__main__':
print"main thread starting..."
run()
print "main thread finished..."
程序结果如下:
[root@localhost test_del]# pythontesttheading.py
main thread starting...
main thread finished...
thread 0: 1
thread 1: 2
thread 2: 3
thread 3: 4
thread 4: 5
thread 5: 6
thread 6: 7
thread 8: 8
thread 7: 9
thread 9: 10
thread 0: 11
thread 1: 12
thread 2: 13
thread 3: 14
没有任何问题。
小结:以上介绍了python中多线程编程的基本使用,包括join, setDaemon和线程锁等。