7.1 thread
Common parameters Yue Ming
Dui indicates wither a target image, i.e., the name of any child Qian name Nu process to drive the sub-bags anti line
args parameter paternity in position in the target function, is a tuple, the parameters must be a comma
Examples of the method used:
-
1.Thread.run (self)
The method of operation when the thread starts, function parameters specified method is called by the target.
-
2.Thread,start (self)
Start the process, start it is to go and you call the run method.
-
3.Thread, terminate (self)
Forced to terminate the thread
-
4.Thread.join (self, timeout=None)
Blocking calls, the main thread waits.
-
5.Thread. setDaeman (self , daemonic)
The guardian of the child thread to thread.
-
6.Thread . getName (self,name)
Get Thread name Thread, setName (self, name) Set the thread name
7.2 multithreaded processes
-
1. The main thread:
When a program starts, there is a thread running, the thread is usually called the application's main thread
-
2. The child thread:
Because the procedure is performed at the outset, if you need to create a thread, the thread that created the child thread the main thread
-
3. The main thread of importance is reflected in two aspects:
-
It is to produce other sub-thread thread
-
Usually it must finalize the implementation of such closed to perform various operations
-
-
join :
Blocking the caller until a call to join () method of the end of the thread execution, will continue down
###### for i in range (10): create a thread to start a thread for i in range (10): block the thread ######
import time
from threading import Thread
def hello (name):
print ('hello,{}'.format(name)) #传参数传的是 元组
time.sleep(3)
print ('666')
def hi ():
print ('wuchi')
time.sleep(3)
print ('888')
if __name__ == '__main__':
hello_thread = Thread(target = hello,args=()) #函数名
hi_thread = Thread(target = hi)
hello_thread.start()
hi_thread.start()
hello_thread.join()
hi_thread.join()
##############
class MyThread( Thread):
def__ init__ (self, people_ name, *args, **kwargs):
super( ).__ init__ ( *args, **kwargs)
self.people_name = people_name
def run( self):
print( 'hello, {}'.format(self.people_name))time .SLEEP ( . 3) Print ( 'byebye') with my_ as Thread = the MyThread ( 'means', name = 'is playing') Print ( my_thread. getName ()) my_thread. Start ().
lock
from threading import Thread,Lock
X=0
n = 1000000
lock = Lock( )
def incr(n):
global x
for i in range(n):
lock. acquire()
X+=1
lock. release()
def decr(n):
global x
for i in range(n):
lock . acquire( )
X-=1
lock. release()
i = Thread(target=incr, args=(n,))
d = Thread( target=decr, args=(n,))
i.start( )
d.start( )
i. join()
d.join()
print(x)
queue
Into the team: put (item)
the team: get ()
test empty: empty () # approximate
test full: full () # approximate
queue length: qsize () # approximate
waiting for completion: join ()
End: task_done ()
from threading import Thread,Lock
from queue import Queue '
from random import randint
my_queue = Queue(10)
def incr(my_queue):
for i in range(10):
num. randint(0,1000)
my_queue. put(num)
def decr(my_queue ):
for i in range(5):
num . my_queue.get()
myPqueue.task_done()
print( num)
i = Thread( target=incr, args=(my_queue,))
d = Thread(target-decr, args=(my_queue,))
i.start( )
d.start( )
i.join( )
d.join()
7.3 thread pool
from multiprocessing.pool import ThreadPool
class ThreadPool(object):
def __init__ ( self, n):
self.q = Queue( )
for i in range(n):
Thread( target=self. worker, daemon= True).start( )
def worker(self):
while True:
func, args, kwargs = self.q.get()
func( *args, **kwargs )
self.q.task_done()
def put(self, target, args=( ), kwargs={} ):
self.q.put((target, args, kwargs))
def join(self):
self.q. join()
def hello( name):
print('hello, lf'. format( name) )
time .sleep(2)
print( ' byebye')
tp = ThreadPool(5)
for i in range(10):
tp.put(hello, args=(i,))
tp. join( )
##########################
def hello(name):
print( 'hello, f]'. format( name))
time . sleep(2)
print( ' byebye' )
tp = ThreadPool(10)
for i in range(10): .
tp. apply_ async(hello, args=(i,))
# tp. terminate()
tp. close( )
tp.