Open thread using the threading module method

Introduce the concept of more than one thread
threading module introduces
threading and multiprocessing module level module in use, there are a lot of similarities.
Second, the multi-threaded open in two ways
1. Create a thread overhead is less than the cost of the process of creating, thus creating a thread faster
from multiprocessing Import Process
from the Thread Threading Import
Import os
Import Time
DEF Work ():
Print ( '<% S> IS running 'os.getpid% ())
the time.sleep (2)
Print (' <% S> IS DONE 'os.getpid% ())
IF name ==' main ':
T = the Thread (target = Work ,)

t= Process(target=work,)

t.start()
print('主',os.getpid())

The first way to open the process
from the Thread Threading Import
Import Time
class Work (the Thread):
DEF the init (Self, name):
Super (). The init ()
self.name name =
DEF RUN (Self):

time.sleep(2)

    print('%s say hell'%self.name)

IF name == ' main ':
T = Work ( 'Egon')
t.start ()
Print ( 'primary')
opening a second embodiment of a thread (in class)
open at a plurality of processes and threads in a process the difference between the opening of a plurality of sub-processes
from process multiprocessing Import
from the Thread Threading Import
Import Time
DEF Work ():
the time.sleep (2)
Print ( 'Hello')
IF name == ' main ':
T = the Thread (target = Work) # If you wait a few seconds, he will first open in the process of printing the main, if unequal will first print hello

t = Process (target = work) # child process will first print master,

t.start()
print('主')

Opening speed is greater than the opening speed of the thread process

2.----------

multiprocessing Import Process from
from the Thread Threading Import
Import os
DEF Work ():
Print ( 'the Hello', os.getpid ())
IF name == ' main ':
# to open multiple threads in the main process related to each thread pid as the main process of
T1 = the thread (target = Work)
T2 = the thread (target = Work)
t1.start ()
t2.start ()
Print ( 'main thread pid', os.getpid ())

#来多个进程,每个进程都有不同的pid
p1 = Process(target=work)
p2 = Process(target=work)
p1.start()
p2.start()
print('主进程pid', os.getpid())

Pid opening in a plurality of processes and a plurality of threads to open under the same process different
Threading Import from the Thread
from multiprocessing Import Process
Import OS
DEF Work ():
Global n-
N-. 1 =
Print (n-) # 99 was changed so that the
= 100 n-
IF name == ' main ':

p = Process(target=work)

p = Thread(target=work)  #当开启的是线程的时候,因为同一进程内的线程之间共享进程内的数据
                        #所以打印的n为99
p.start()
p.join()
print('主',n) #毫无疑问子进程p已经将自己的全局的n改成了0,
# 但改的仅仅是它自己的,查看父进程的n仍然为100

Threads share data of the process in the same process
between processes are isolated from each other and do not share. We need to use a third party to complete the share (by means of queues, pipes, shared data)

Third, exercise

Exercise 1: multi-threaded concurrency

from socket import
from threading import Thread
s = socket(AF_INET,SOCK_STREAM)
s.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) #端口重用
s.bind(('127.0.0.1',8081))
s.listen(5)
print('start running...')
def talk(coon,addr):
while True: # 通信循环
try:
cmd = coon.recv(1024)
print(cmd.decode('utf-8'))
if not cmd: break
coon.send(cmd.upper())
print('发送的是%s'%cmd.upper().decode('utf-8'))
except Exception:
break
coon.close()
if name == 'main':
while True:#链接循环
coon,addr = s.accept()
print(coon,addr)
p =Thread(target=talk,args=(coon,addr))
p.start ()
S.CLOSE ()
server
from Socket Import

C = Socket (AF_INET, SOCK_STREAM)
c.connect (( '127.0.0.1', 8081))
the while True:
cmd = INPUT ( '>>:') .strip ()
IF Not cmd: Continue
c.send (cmd.encode ( 'UTF-. 8'))
Data = c.recv (1024)
Print ( 'accepted% s'% data.decode (' utf -8 '))
c.close ()
client
exercise II: three tasks, receiving a user input, the contents of a user's input to uppercase format, a result formatted into a file

from threading import Thread
import os
input_l = []
format_l = []
def talk(): #监听输入任务
while True:
cmd = input('>>:').strip()
if not cmd:continue
input_l.append(cmd)

the format DEF ():
the while True:
IF input_l:
RES = input_l.pop () # taken out
format_l.append (res.upper ()) # taken out after uppercase variants
DEF Save ():
the while True:
IF format_l: If # format_l not empty
with Open ( 'DB', 'A') aS F:
f.write (format_l.pop () + '\ n-') written into the file #
f.flush ()
IF name == ' main ':
the thread = T1 (target = Talk)
T2 = the thread (target = format)
T3 = the thread (the Save target =)
t1.start ()
t2.start ()
t3.start ()
answer
four multiple threads within the same process share address space
function () {// MT4 tutorial: www.kaifx.cn/mt4.html
from the Thread Threading Import
from multiprocessing Import Process
Import OS
n-100 =
Talk DEF ():
Global n-
N- 100 =
Print (n-)
IF name == ' main ':
T = the Thread (target = Talk) # If the thread is turned on, then, n = 0

t = Process (target = talk) # If the process is open, then, n = 100

t.start()
t.join()
print('主',n)

Five other thread object properties and methods

Method Thread object instance

isAlive (): Returns the thread is active.

getName (): Returns the thread name.

setName (): Set the thread name.

Some methods provided by threading module:

threading.currentThread (): Returns the current thread variable.

threading.enumerate (): Returns a list of running threads. Refers to the thread starts running, before the end, does not include a thread before starting and after termination.

threading.activeCount (): Returns the number of running threads, and len (threading.enumerate ()) have the same result.

from threading import Thread
from multiprocessing import Process
import time,os,threading
def work():
time.sleep(2)
print('%s is running' % threading.currentThread().getName())
print(threading.current_thread()) #其他线程
print(threading.currentThread().getName()) #得到其他线程的名字
if name == 'main':
t = Thread(target=work)
t.start()

print(threading.current_thread().getName())  #主线程的名字
print(threading.current_thread()) #主线程
print(threading.enumerate()) #连同主线程在内有两个运行的线程
time.sleep(2)
print(t.is_alive()) #判断线程是否存活
print(threading.activeCount())
print('主')

Other properties and methods of thread
six, join the daemon thread

All non-primary guardian of the child process before the end of the process such as the end of his (recovery of its resources sub-processes) :( parent-child relationship)
the main thread and other non-daemon thread to end it all ended: (no parent-child relationship)

Threading the Thread Import from
Import Time, OS
DEF Talk ():
the time.sleep (. 3)
Print ( '% S IS running ..' os.getpid% ())
IF name == ' main ':
T = the Thread (target = Talk)
t.start ()
t.join child process () # main processes such as the end of the
print ( 'primary')

join
difference daemon thread and daemon

1. daemons: the main process will wait until the end of all non-daemon process, destroys the daemon. That (the primary process running over the guard that will get rid of)

2. daemon threads: the main thread running that has not yet finished the guardian kill, the main thread and other non-daemon thread to end it all ended

from multiprocessing import Process
from threading import Thread,currentThread
import time,os
def talk1():
time.sleep(2)
print('hello')
def talk2():
time.sleep(2)
print('you see see')
if name == 'main':
t1 = Thread(target=talk1)
t2 = Thread(target=talk2)

t1 = Process(target=talk1)

# t2 = Process(target=talk2)
t1.daemon = True
t1.start()
t2.start()
print('主线程',os.getpid())

Daemons and daemon threads
examples # 3 -------- confuse the people
from the Thread Threading Import
Import Time
DEF foo ():
Print (123)

time.sleep (10) #, etc. If a time greater than following the like, do not put the printed end123

time.sleep(2)  #如果这个等的时间小于下面等的时间,就把end123也打印了
print('end123')

def bar():
print(456)

time.sleep(5)

time.sleep(10)
print('end456')

IF name == ' main ':
T1 = the Thread (target = foo)
T2 = the Thread (target = bar)
t1.daemon # = True main thread that runs daemon has not finished kill,

The main thread and other non-daemon thread to end it all ended

t1.start()
t2.start()
print('main---------')

An example of a tempting
seven, GIL and Lock

1.python GIL (Global Interpreter Lock) # global interpreter lock

2. The purpose of the lock: the expense of efficiency, ensure the security of data
3. Protect plus various different data lock ()
4.python own garbage collection

5. Who got permission to perform the GIL lock let anyone get Cpython interpreter

6.GIT lock protection is safe Cpython interpreter of data, but does not protect the security of your own data program
7.GIL lock when faced with obstruction, forced it to release the lock, then the other on began to grab the lock, grab
it after the value of the modified, but the first to get it to stay still had to get that data yet, when once again take
to lock when the data has been modified, and you also get original, so confusing, so we can not guarantee the
security of the data.
8. So how to solve data security ne?
Own to give it locks plus: mutex = Lock ()

Genlock

GIL and Lock are two locks, protection of data is not the same, the former interpreter level (of course, is to protect the interpreter-level data, such as data garbage collection), which is the application of data protection users to develop their own , it is clear that GIL is not responsible for this matter, can only handle user-defined lock that Lock

Process Analysis: grab all the threads that GIL lock, or grab all threads are executing authority

Thread 1 grab GIL lock, get execute permissions, execution, and then add a handful Lock, has not yet finished, that thread 1 has not been released Lock, thread 2 is likely to grab GIL lock, begin the process of execution Lock has not been found to release the thread 1, thread 2 then enters the blocked execute permissions are taken away, it is possible to get the thread 1 GIL, and then perform normal to release Lock. . . This leads to the effect of serial operation

Since it is serial, then we execute

t1.start()

t1.join

t2.start()

t2.join()

This is the serial execution ah, why add Lock it, Know join t1 is waiting for all of the code executed, the equivalent of all the code lock t1, and Lock code is only part of the operation to lock shared data.

Because the Python interpreter to help you to automatically recover memory regularly, you can understand python interpreter has a separate thread, every once in a while it did wake up from a global poll to see which memory data can be cleared, at this point in the program of your own threads and py interpreter own thread is running concurrently, if you delete a variable thread, the thread garbage collection py interpreter clearing moment in the process of emptying this variable, it may be a other threads that just has not come yet again to clear the memory space and was assigned a result it is possible to assign new data has been deleted, in order to solve similar problems, python interpreter simple and crude, locking that is, when a thread running, other people do not move, this would resolve the above problems, it can be said that the legacy of earlier versions of Python.

from threading import Thread,Lock
import time
n=100
def work():
mutex.acquire()
global n
temp=n
time.sleep(0.01)
n=temp-1
mutex.release()
if name == 'main':
mutex=Lock()
t_l=[]
s=time.time()
for i in range(100):
t=Thread(target=work)
t_l.append(t)
t.start()
for t in t_l:
t.join()
print('%s:%s' %(time.time()-s,n))

Global Interpreter Lock
locks are typically used to synchronize access to shared resources. Lock create a shared resource to each object, when you need to access the resource, call acquire methods to get the lock object (if another thread has already acquired the lock, then the current thread needs to wait for it to be released) until after the resource access and then call the release method to release the lock:

Threading Import
the mutex of threading.Lock = ()
mutex.aquire ()
'' '
operating on the common data
' ''
mutex.release ()

Format lock
1 analysis:
22 1.100 grab GIL lock threads that execute permissions grab
33 2. there must be a thread to grab GIL (for the time being called threads 1), and begins execution, it will get executed once lock.acquire ()
4 4 3. thread 1 is most likely not yet finished running, there is another thread 2 grab GIL, and then began to run, but found that a mutex lock thread 2 thread 1 has not yet been released, then blocked , was forced to hand over authority to perform, namely the release of GIL
5 5 4. 1 until the thread again grabbed the GIL, execution continues from the last pause position until the normal release the mutex lock, then other threads to repeat 234 of course
if you do not lock: concurrent execution, speed and data secure.

Lock: serial execution, slow and data security.

# Unlocked: concurrent execution speed, data is insecure
from Threading Import current_thread, the Thread, Lock
Import OS, Time
DEF Task ():
Global n-
Print (. 'IS running S%'% current_thread () getName ())
= n-TEMP
the time.sleep (0.5)
n--TEMP. 1 =

if name == 'main':
n=100
lock=Lock()
threads=[]
start_time=time.time()
for i in range(100):
t=Thread(target=task)
threads.append(t)
t.start()
for t in threads:
t.join()

stop_time=time.time()
print('主:%s n:%s' %(stop_time-start_time,n))

'''
Thread-1 is running
Thread-2 is running
......
Thread-100 is running
主:0.5216062068939209 n:99
'''

# Unlock: unlocked concurrent execution part, lock part serial execution, slow, data security
from Threading Import current_thread, the Thread, Lock
Import os, time
DEF Task ():
# unlocked code to run concurrently
time .sleep (. 3)
Print ( '% S Start to rUN' current_thread% (). getName ())
Global n-
# lock serial code run
lock.acquire ()
TEMP = n-
the time.sleep (0.5)
n-TEMP = -1
lock.release ()

if name == 'main':
n=100
lock=Lock()
threads=[]
start_time=time.time()
for i in range(100):
t=Thread(target=task)
threads.append(t)
t.start()
for t in threads:
t.join()
stop_time=time.time()
print('主:%s n:%s' %(stop_time-start_time,n))

'''
Thread-1 is running
Thread-2 is running
......
Thread-100 is running
主:53.294203758239746 n:0
'''

# Some students may have a question: Since the lock will become a serial run, then I use the join immediately after the start, you do not lock, ah, ah effect is serial
# right: use immediately after start jion, certainly will execute the task becomes 100 serial, without a doubt, the result of the final n is 0 certainly is safe, but the problem is to
join immediately after #start: all the code within the tasks are serial execution, and locked, but the lock section that is part modify shared data is serial
# from single to ensure data security, both can be achieved, but it is clearly higher efficiency locked.
from Threading Import current_thread, the Thread, Lock
Import OS, Time
DEF Task ():
the time.sleep (. 3)
Print ( '% S Start to RUN'% current_thread () getName ().)
Global n-
TEMP = n-
the time.sleep (0.5)
n- = temp-1

if name == 'main':
n=100
lock=Lock()
start_time=time.time()
for i in range(100):
t=Thread(target=task)
t.start()
t.join()
stop_time=time.time()
print('主:%s n:%s' %(stop_time-start_time,n))

'' '
The Thread Start. 1-RUN to
the Thread Start-2 to RUN
......
to RUN-100 the Thread Start
Main: 350.6937336921692 n: 0 # terror is how time consuming
' ''
differences and join the mutex (emphasis !!!)

Guess you like

Origin blog.51cto.com/14511863/2449782