Back to index

python3.2  3.2.2
Functions
mp_workers Namespace Reference

Functions

def worker
def calculate
def mul
def plus
def test

Function Documentation

def mp_workers.calculate (   func,
  args 
)

Definition at line 32 of file mp_workers.py.

00032 
00033 def calculate(func, args):
00034     result = func(*args)
00035     return '%s says that %s%s = %s' % \
00036         (current_process().name, func.__name__, args, result)
00037 
00038 #
00039 # Functions referenced by tasks
00040 #

Here is the caller graph for this function:

def mp_workers.mul (   a,
  b 
)

Definition at line 41 of file mp_workers.py.

00041 
00042 def mul(a, b):
00043     time.sleep(0.5*random.random())
00044     return a * b

def mp_workers.plus (   a,
  b 
)

Definition at line 45 of file mp_workers.py.

00045 
00046 def plus(a, b):
00047     time.sleep(0.5*random.random())
00048     return a + b
00049 
00050 #
00051 #
00052 #

def mp_workers.test ( )

Definition at line 53 of file mp_workers.py.

00053 
00054 def test():
00055     NUMBER_OF_PROCESSES = 4
00056     TASKS1 = [(mul, (i, 7)) for i in range(20)]
00057     TASKS2 = [(plus, (i, 8)) for i in range(10)]
00058 
00059     # Create queues
00060     task_queue = Queue()
00061     done_queue = Queue()
00062 
00063     # Submit tasks
00064     for task in TASKS1:
00065         task_queue.put(task)
00066 
00067     # Start worker processes
00068     for i in range(NUMBER_OF_PROCESSES):
00069         Process(target=worker, args=(task_queue, done_queue)).start()
00070 
00071     # Get and print results
00072     print('Unordered results:')
00073     for i in range(len(TASKS1)):
00074         print('\t', done_queue.get())
00075 
00076     # Add more tasks using `put()`
00077     for task in TASKS2:
00078         task_queue.put(task)
00079 
00080     # Get and print some more results
00081     for i in range(len(TASKS2)):
00082         print('\t', done_queue.get())
00083 
00084     # Tell child processes to stop
00085     for i in range(NUMBER_OF_PROCESSES):
00086         task_queue.put('STOP')
00087 

Here is the call graph for this function:

def mp_workers.worker (   input,
  output 
)

Definition at line 23 of file mp_workers.py.

00023 
00024 def worker(input, output):
00025     for func, args in iter(input.get, 'STOP'):
00026         result = calculate(func, args)
00027         output.put(result)
00028 
00029 #
00030 # Function used to calculate result
00031 #

Here is the call graph for this function: