Python Threads and processes

Source: Internet
Author: User

Python Threads and processes (process)

Process: A collection of resources that itself does not execute
Threads: A sequence of instructions

Thread shared memory, process independent memory, process start slow, thread start fast, data between threads can communicate, data between processes can not communicate.

Daemon process

Normally the main thread is not dependent on the child thread, the program executes, and you go I also go. The Join method is used in the double thread so that the main thread waits for the child thread to finish executing. The child thread setting Setdaemon (True) sets the current thread as the daemon thread, which is unimportant and must be set before start. Python interpreter only releases one thread at a time

Gil Lock (Global interpreter Lock)

Interpreter exit control, at the same time only one thread can work. The rest of the threads are waiting. The thread that starts is actually on the CPU, but only one thread can get the data at the same time. Global interpreter lock.

Thread Lock (Mutex)

Threading. Lock () Lock.acquire () lock.release ()

Recursive lock

Threading. Rlock () Multiple doors are locked, keys cannot be identified by the corresponding door

Semaphore (semaphore)

Semaphore=threading. Boundedsemaphore (5) #最多允许5个线程同时运行
Semaphore.acquire () lock.release () controls how many threads are allowed to execute at the same time, instance, thread pool, process pool

Event
  

Event=threading. Event ()
event.wait ()
Event.set ()
Event.clear ()
Author = ' mr.bool '
Import time
Import Threading
Event=threading. Event ()
def lighter ():
count=0
Event.set ()
while True:
If count> 5 and Count<10:
Event.clear () #变红灯
Print ("Red light")
Elif count>10:
Event.set () #变绿灯
Print ("Turn green")
count=0
Else:
Print ("Now is green")
Time.sleep (1)
Count+=1


def car (name):
While T Rue:
If Event.is_set ():
Print ("%s can pass"%name)
Else:
Print ("%s is not reachable"%na Me)
event.wait ()
Time.sleep (1)
T1=threading. Thread (Target=lighter)
T1.start ()
T2=threading. Thread (target=car,args= (' Benz ',))
T2.start ()
Queue

Advantages: Decoupling efficiency
Queue. Queue () Advanced First Out
Queue. Lifoqueue () LIFO buy fruit
Queue. Priorityqueue () can set priority when storing data
Put (position, data) #put (1, ' WJ ') to place data in the first bit

    author = ‘Mr.Bool‘
import queue
import time
import threading
q=queue.Queue()
def Proceder():
count =0
while True:
q.put("骨头%s"%count)
print("生产了骨头%s"%count)
count+=1
time.sleep(1)
def eat(name):
while True:
g=q.get()
print("%s ear %s"%(name,g))
time.sleep(0.3)


p=threading.Thread(target=Proceder)
p.start()
d1=threading.Thread(target=eat,args=(‘wangwang‘,))
d1.start()
d2=threading.Thread(target=eat,args=(‘wangcai‘,))
d2.start()

Multi-process

Import multiprocessing
There are process,pool,queue,pipe,manager in multiprocessing.
Process Initiation Processes
Pool Create process pool when using pool must first close (), after join ()
Queue data interaction
Pipe Pipeline Data interaction
Manager data sharing, you can create new dict,list and other shared use instances:
1. Process

import multiprocessing
import time,threading
import os
def threadingrun(name):
# print("%s is a big man"%name)
pass
def run(name):
print("now pid:%s"%os.getpid())
print("parent pid:%s"%os.getppid())
t=threading.Thread(target=threadingrun,args=(name,))
t.start()
if __name__ == ‘__main__‘:
for i in range(10):
p1=multiprocessing.Process(target=run,args=("wj",))
p1.start()
  1. Poll
    from multiprocessing import Pool
    import os
    import time
    def processingtest(s):
    time.sleep(2)
    print("in process",os.getpid())
    return s+100
    def Bar(arg):
    print(‘-->exec done:‘,arg,os.getpid())
    pass
    name=="_main":
    pool=Pool(3)
    print("主进程")
    for i in range(10):
    pool.apply
    async(func=processingtest,args=(i,),callback=Bar)
    print(‘end‘)
    pool.close()
    pool.join()
  2. Queue:

    from multiprocessing import Process,Queue,Pipe,Manager
    def processingtest(s):
    s.put([32,‘hello‘])
    print(‘子进程‘,s.get())


    if name=="__main":
    parent
    pipe,child
    pipe=Pipe()
    q=Queue()
    p=Process(target=processingtest,args=(q,))
    p.start()
    print(‘父进程‘,q.get())

  3. pipe:
      from multiprocessing import Process,queue,pipe,manager 
    def processingtest (Child Pipe ):
    Child
    pipe.send ("Hello parent")
    Print (Child pipe.recv ())
    If name = = "
    Main ":
    Parent
    pipe,child
    pipe=pipe ()
    P=process (target=processingtest,args= (Child
    Pipe,))
    P.start ()
    Print (Parent pipe.recv ())
    Parent
    pipe.send ("Hello son")
  4. Manager:
    from multiprocessing import Process,Manager
    import os
    def processingtest(d,l):
    d[os.getpid()]=os.getpid()
    l.append(os.getpid())
    pass
    name=="_main": with Manager() as manager:
    d=manager.dict()
    l=manager.list()
    obj
    list=[]
    for i in range(10):
    p=Process(target=processingtest,args=(d,l))
    p.start()
    obj
    list.append(p)
    for i in range(10):
    obj
    list[i].join()
    print(d)
    print(l)

Python Threads and processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.