Queue data sharing problem in Python multi-process

Source: Internet
Author: User

0x00 up

Today, when writing a small thing, you need to control concurrency, but you can't call Python multiprocessing directly (the problem is mentioned later in this article). So try to use queue to achieve.

The idea at the beginning is this:

 fromMultiprocessingImportProcess fromQueueImportQueueq= Queue (maxsize = 10)#adding data to a queue from a web appdefput (num): Q.put (num)defRead (): whileTrue:PrintQ.get ()if __name__=='__main__': Process (Target=read, args= ())

The data for the queue is added from the Web application (the code that omits the bottle), a process is opened, the data is continuously read from the queue and processed (omitting the process).

The logic is correct, but in the actual test, a problem is found.

The Queue.get () function is a default blocking function that waits if the queue is empty, similar to SOCKET.RECV. At the time of the test, the program was stuck here, meaning that the read () function did not read the data in the queue.

0X10 Bearing

To solve this problem, I modified the code to print the relevant information:

#Encoding:utf-8 fromMultiprocessingImportProcess fromQueueImportQueueq= Queue (maxsize = 10)#adding data to a queue from a web appdefput (num): Q.put (num)defRead ():Printq.qsize ()#While True:    #print q.get ()if __name__=='__main__': Put (2333)    Printq.qsize () Process (target=read, args= ()). Start ()

The printed result is 1 0. The queue in the newly opened process is empty indeed.

To check the data (http://my.oschina.net/yangyanxing/blog/296052), the explanation is that the queue object cannot communicate between the parent and child processes

0x20 turn

Finally learned that multiprocessing provides a queue for call, can solve this problem perfectly.

#Encoding:utf-8 fromMultiprocessingImportProcess, Queueq= Queue (10)#adding data to a queue from a web appdefput (num): Q.put (num)defRead (): whileTrue:PrintQ.get ()if __name__=='__main__': Put (2333)    Printq.qsize () Process (target=read, args= ()). Start ()

0X30 Co

As mentioned above, in the bottle can not use the multiprocessing, a little check, the reason is that the thread is unable to open the process. It's not quite understood.

Queue data sharing problem in Python multi-process

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.