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