Python inter-process communication usage example
This example describes the communication usage between Python processes. Share it with you for your reference. The details are as follows:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 |
#! /Usr/bin/env python
#-*-Coding = UTF-8 -*-
Import multiprocessing
Def counsumer (input_q ):
While True:
Item = input_q.get ()
# Processing Projects
Print item # replace this with a useful job
# Signal notification task completed
Input_q.task_done ()
Def producer (sequence, output_q ):
For item in sequence:
# Put a project into a queue
Output_q.put (item)
# Creating a process
If _ name _ = '_ main __':
Q = multiprocessing. JoinableQueue () # create a connectable shared process queue
Cons_q = multiprocessing. Process (target = counsumer, args = (q ,))
Cons_q.daemon = True
Cons_q.start ()
Sequence = [1, 2, 4]
Producer (sequence, q)
Q. join ()
Print 'success'
|
I hope this article will help you with Python programming.