Python Multi-process multithreaded data sharing

Source: Internet
Author: User

Tag:python    thread queue.queue    process multiprocess.queue   

#!/usr/bin/env python# -*- coding:utf-8 -*-# author: changhua gongfrom  multiprocessing import process, queueimport os, time, random ' 1.  We usually from queue import queue are thread-to-column, used for data sharing, and can only be used between threads;2. from multiprocessing  Import queue, which is a process-to-column, is used for inter-process data exchange, in fact the same data is modified between processes for serialization and deserialization (pickle)     completion of data interaction;3.  threads need to be locked , while data transfer between processes is only transitive (data sharing). "' #  Write Data Process Execution code: def write (q):     print (' process to write: %s '  % os.getpid ())     for value in [' A ',  ' B ',  ' C ']:         print (' Put %s to queue ... '  % value)          q.put (value)   #  push          time.sleep (Random.random ()) #  read data Process Execution code: def read (q):     print ( ' Process to read: %s '  % os.getpid ())     while True:         value = q.get (True)   #  get          print (' Get %s from queue. ')  % value) if __name__== ' __main__ ':    #  parent process creates a queue and passes it to each child process:     q = queue ()     pw = process (target=write, args= (q,))     pr = process (target=read, args= (q,))     #  Start child process PW, write:     pw.start ()     #  start child process PR, read:     pr.start ()     #  wait for PW to end:     pw.join ()      # PR process is a dead loop, unable to wait for its end, can only forcibly terminate:     pr.terminate ()          #!/usr/bin/env python# -*- coding:utf-8 -*-# author: changhua gongfrom multiprocessing import process, pipefrom  Time import sleepfrom multiprocessing import freeze_support ' Def Pipe (duplex= True):     return connection (),  connection () explains the problem: the pipe can only interact between two processes, like the phone line form and send and receive "Def  RUN_A (conn):     for i in range (3):         conn.send ("i am from run_a: %s"  % i)          sleep (0.3)     for i in range (5):         print (Conn.recv ()) Def run_b (conn):    for i  In range (3):         print (Conn.recv ())     for  i in range (5):         conn.send ("I am from  run_b: %s "&nbsP;%  i)         sleep (0.3) if __name__ ==  "__ Main__ ":     freeze_support ()     conn1, conn2 = pipe ()     pa = process (target=run_a, args= (CONN1,))     PB  = process (target=run_b, args= (CONN2,))     pa.start ()      pb.start ()     pa.join ()     pb.join ()

This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1927751

Python Multi-process multithreaded data sharing

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.