Categories
Uncategorized

python network thread

Thread

A thread is a multi-tasking programming mode, you can use a multi-core computer resources. Also known as the thread lightweight process

Thread features

* A thread is the smallest unit of computer core allocation process * A process can contain multiple threads * thread is running, but also consume computer resources. Process multiple threads share its resources and space * thread also has its own unique resource properties, such as instruction set, TID, etc. * Whether to create or delete a thread or process running resource consumption is less than * parallel execution across multiple threads, and do not interference

threading threading module

from threading import Thread

t = Thread(target, [, args], [kwargs])

Create a thread object

    target binding thread function

    tuple args parameter passing position to the thread function

    kwargs dictionary to the thread function key parameter passing

t.start () start the thread

t.join ([timeout]) collection threads

import threading
import os

a = 1

#

Thread function

def music(): print("

Process pid No.

", os.getpid()) global a print("a = ",a) a = 10000 t = threading.Thread(target=music) #

Create a thread object

t.start() #

Start a thread

print("

Process pid No.

", os.getpid()) t.join() #

Recycling thread

print("Main a:",a) #

Process pid No. 12549

#

Process pid No. 12549

# a = 1 # Main a: 10000

os.getpid get that number pid process, thread is a member of the process.

Variable thread change is the process variables. It did not open up a new space.

Thread Attributes

t.is_alive () View thread state

t.name name of the default threads Thread-1

t.setName () Set the thread name

threading.currentThread () Gets the current thread object

 1 from threading import Thread,currentThread
 2 from time import sleep 
 3 
 4 #

Thread function

5 def fun(sec): 6 print("

Thread Attributes Test

") 7 sleep(sec) 8 #

Gets thread object getName () Get the name

9 print("

% S thread end

"%currentThread().getName()) 10 11 thread = [] 12 13 for i in range(3): 14 t = Thread(target = fun,name = "tedu%d"%i,\ 15 args = (3,)) 16 thread.append(t) 17 t.start() 18 print(t.is_alive()) #

View the process status

19 20 thread[1].setName('Tarena') #

Set the thread name

21 print(thread[2].name) #

Get Thread name

22 23 #

Recycling thread

24 for i in thread: 25 i.join() 26 27 #

Thread Attributes Test

28 # True 29 #

Thread Attributes Test

30 # True 31 #

Thread Attributes Test

32 # True 33 # tedu2 34 #

Tarena end of the thread

35 #

tedu0 end of the thread

36 #

tedu2 end of the thread

View Code

t.daemon

By default, the end of the main thread does not affect threads branch, set to True if the main thread exit branch thread will quit

Setting method:

t.daemon = True

t.setDaemon()

Set the thread daemon property before the start; after general settings daemon does not use the join

from threading import Thread
from time import sleep

def fun():
  sleep(3)
  print("

Thread Attributes Test

") t = Thread(target=fun, name = "Tarena") #

The main thread exit branch threads also quit

t.setDaemon(True) t.start() t.setName("Tedu") print("Name:",t.getName()) #

Thread name

print("Alive:",t.is_alive()) #

Thread Life Cycle

print("is Daemon",t.isDaemon()) #

With the main branch process exit process

Custom thread class

    Thread class inheritance

    __init__ method of operation for the Thread class parent class attributes

    Override the run method

Instructions

    Instantiate an object

    Automated call start method executes the run

    Call to join the recovery thread

from threading import Thread

class ThreadClass(Thread):
  #

Override the parent class init

def __init__(self, *args, **kwargs): self.attr = args[0] super().__init__() #

Load the parent class init

def fun1(self): print("

Function 1

") def fun2(self): print("

Function 2

") #

Rewrite run, the logical call

def run(self): self.fun1() self.fun2() t = ThreadClass("abc") t.start() t.join() #

Function 1

#

Function 2

Synchronization mutex

Inter-thread communication method

1. The communication method: using global variables between threads communicate

2. Shared resource contention

    Sharing resources: multiple processes or threads can operate is called a shared resource. Operation code segment to a shared resource called the critical zone.

    Impact: disorderly operation of shared resources could bring chaos data, or operator error. At this time, often require synchronization mechanism coordinating the sequence of operations are mutually exclusive.

3. Synchronize mutual exclusion mechanism

Synchronization: Synchronization is a collaborative relationship, to complete the operation, between multiple processes or threads to form a coordinated, orderly steps necessary to perform the operation.

Mutex: mutual exclusion is a constraint relationship, when a process or thread locking possession of resources will be processed at this time other process threads can not operate the resources to operate until after unlocking.

Mutex thread synchronization method

Thread Event

from threading import Event

e = Event () creates a thread event objects

e.wait ([timeout]) blocked waiting to be set e

e.set () provided e, so that the end blocking wait

e.clear () is not set back to the state that the e

e.is_set () is set or view the current e

from threading import Thread,Event

s = None  #

For communication

e = Event() #

Create event objects

def 杨子荣(): print("

Zirong come to worship the hills

") global s s = "

King cover ground tiger

" e.set() #

Set of e

t = Thread(target=杨子荣) t.start() print("

Said password is one of us

") e.wait() #

Blocking wait password say

if s == '

King cover ground tiger

': print("

River town pagoda demon

") print("

Confirmed eyes, you are the right person

") else: print("

kill him...

") t.join()

Thread Lock Lock

from threading import Lock

lock = Lock () to create a lock object

lock.acquire () If the lock is already locked locked again call blocks

lock.release () Unlock

with lock: # lock

automatically unlock code block ends with

from threading import Thread,Lock

a = b = 0
lock = Lock() #

Defined lock

def value(): while True: lock.acquire() #

Lock

if a != b: print("a = %d,b = %d"%(a,b)) lock.release() #

Unlock

t = Thread(target = value) t.start() while True: #

Lock

with lock: a += 1 b += 1 #

Auto Unlock

t.join()

GIL problem python thread

The GIL (Global Interpreter Lock)

python — “supports threaded operation —” synchronization and mutual exclusion of IO – “locked —-” super lock to lock interpreter

Consequences: an interpreter, explained only one thread at a time, then other threads need to wait. Greatly reducing the efficiency of the implementation python thread

python GIL solutions to problems * modified * c interpreter to make use of multiple processes in parallel operation * python thread can use high latency IO case of a multi obstruction * do not use cpython c # java do interpreter

Efficiency test

Were tested multi-process multi-threaded single process to perform the same operation IO and CPU

#

Computationally intensive

def count(x,y): c = 0 while c < 7000000: x += 1 y += 1 c += 1 #

io-intensive

def write(): f = open("test.txt",'w') for x in range(2000000): f.write("hello world\n") f.close() def read(): f = open("test.txt") lines = f.readlines() f.close()

View Code

Time operation

#

Single-process procedures

from test import * import time # t = time.time() # for i in range(10): # count(1,1) # print("Line cpu:",time.time() - t) t = time.time() for i in range(10): write() read() print("Line IO:",time.time() - t)

View Code

Line cpu: 8.15166711807251
Line IO: 6.841825246810913

from test import * 
import threading 
import time 

counts = []

t = time.time()

for x in range(10):
    th = threading.Thread(target = count,args = (1,1))
    th.start()
    counts.append(th)

for i in counts:
    i.join()
print("Thread cpu",time.time() - t)

View Code

from test import * 
import threading 
import time 

counts = []

def io():
    write()
    read()

t = time.time()

for x in range(10):
    th = threading.Thread(target = io)
    th.start()
    counts.append(th)

for i in counts:
    i.join()
print("Thread IO",time.time() - t)

View Code

Thread cpu 8.414522647857666
Thread IO 6.023292541503906

from test import * 
import multiprocessing 
import time 

counts = []

t = time.time()

for x in range(10):
    th = multiprocessing.Process\
    (target = count,args = (1,1))
    th.start()
    counts.append(th)

for i in counts:
    i.join()
print("Process cpu",time.time() - t)

View Code

from test import * 
import multiprocessing 
import time 

counts = []

def io():
    write()
    read()

t = time.time()

for x in range(10):
    th = multiprocessing.Process(target = io)
    th.start()
    counts.append(th)

for i in counts:
    i.join()
print("Process IO",time.time() - t)

View Code

Process cpu 4.079084157943726
Process IO 3.2132551670074463

Process and thread the differences and connections

    Both are multi-task programmatically, you can use multi-core computer

    Creating than deleting threads of a process consumes more computer resources

    Independent process space, data security is good, there is a special inter-process communication method

    Thread communication using global variables, more simple, but requires synchronization mutex operation

    A process can contain multiple threads, threads share the process of space resources

    Process threads execute independently, with its own unique resources such as property, id, command sets, etc.

Usage:

    A process more concurrent tasks, relatively simple, suitable for use multithreading

    If the data is more complex procedures, especially multiple tasks may be more communication time, taking into account the complexity of the synchronization exclusive use of threads

    There are obvious differences in multiple tasks, and when the functional separation is not necessarily written to a process

    Consider using python threading issues GIL

Leave a Reply