如何使用Threading
Python中的Thread,Threading模块可用于多线程。Threading模块是Thread的封装。
一般而言,使用Threading模块即可。
# -*- coding: utf-8 -*-
#
import thread
def run(n):
for i in range(n):
print i
print thread.start_new_thread(run,(4,))
print thread.start_new_thread(run,(2,))
print thread.start_new_thread(run,(),{'n':4})
# -*- coding: utf-8 -*-
#
import threading
class mythread(threading.Thread):
def __init__(self, num):
threading.Thread.__init__(self)
self.num = num
def run(self):
print 'I am ', self.num
t1 = mythread(1)
t2 = mythread(2)
t3 = mythread(3)
print t1.start()
print t2.start()
print t3.start()
# -*- coding: utf-8 -*-
#
import threading
def run(x, y):
for i in range(x, y):
print i
t1 = threading.Thread(target=run, args=(15, 20))
t1.start()
t2 = threading.Thread(target=run, args=(7, 11))
t2.start()
# -*- coding: utf-8 -*-
#
import threading
import time
class Mythread(threading.Thread):
def __init__(self, id):
threading.Thread.__init__(self)
self.id = id
def run(self):
x = 0
time.sleep(5)
print self.id
def func():
t.start()
t.join()
for i in range(5):
print i
t = Mythread(2)
func()
# -*- coding: utf-8 -*-
#
import threading
import time
class Mythread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name=threadname)
def run(self):
time.sleep(5)
print self.getName()
def func1():
t1.start()
print "func1 done"
def func2():
t2.start()
print "func2 done"
t1 = Mythread("t1")
t2 = Mythread("t2")
t2.setDaemon(True)
func1()
func2()
线程同步
为了保证多个线程共同对某个数据进行修改造成的不可预料的后果,需要对多个线程进行同步。使用Thread对象的Lock和RLock可以实现简单的线程同步。
# -*- coding: utf-8 -*-
#
import threading
import time
class mythread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name = threadname)
def run(self):
global x
lock.acquire()
for i in range(3):
x = x+1
time.sleep(2)
print x
lock.release()
lock = threading.RLock()
t1 = []
for i in range(10):
t = mythread(str(i))
t1.append(t)
x=0
for i in t1:
i.start()
# -*- coding: utf-8 -*-
#
import threading
class Producer(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name=threadname)
def run(self):
global x
con.acquire()
if x == 1000000:
con.wait()
pass
else:
for i in range(1000000):
x = x + 1
con.notify()
print x
con.release()
class Consumer(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name=threadname)
def run(self):
global x
con.acquire()
if x == 0:
con.wait()
pass
else:
for i in range(1000000):
x = x - 1
con.notify()
print x
con.release()
con = threading.Condition()
x=0
p = Producer('Producer')
c = Consumer('Consumer')
p.start()
c.start()
p.join()
c.join()
print x
线程间通讯
Python提供了Event对象用于线程间的相互通讯。Event对象是由线程设置的信号标志,如果信号标志为真,则其他线程等待直到信号解除。Event对象包含了设置信号、清楚信号、等待等用于实现线程间的通讯。
# -*- coding: utf-8 -*-
#
import threading
class mythread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name = threadname)
def run(self):
global event
if event.isSet():
event.clear()
event.wait()
print self.getName()
else:
print self.getName()
event.set()
event = threading.Event()
event.set()
t1 = []
for i in range(10):
t = mythread(str(i))
t1.append(t)
for i in t1:
i.start()
什么是Threading
进程是操作系统中应用程序的执行实例,而线程是进程内部的一个执行单元。
当系统创建一个进程后,也就创建了一个主线程。
每个进程至少有一个线程,也可以有多个线程。
在程序中使用多线程可以实现并行处理,充分利用CPU。
Python提供了对多线程的支持。在Python中可以方便地使用多线程编程。