Back to index

python3.2  3.2.2
Functions | Variables
mp_synchronize Namespace Reference

Functions

def value_func
 TEST_VALUE.
def test_value
def queue_func
 TEST_QUEUE.
def test_queue
def condition_func
 TEST_CONDITION.
def test_condition
def semaphore_func
 TEST_SEMAPHORE.
def test_semaphore
def join_timeout_func
 TEST_JOIN_TIMEOUT.
def test_join_timeout
def event_func
 TEST_EVENT.
def test_event
def sharedvalues_func
 TEST_SHAREDVALUES.
def test_sharedvalues
def test

Variables

 namespace = multiprocessing

Function Documentation

TEST_CONDITION.

Definition at line 75 of file mp_synchronize.py.

00075 
00076 def condition_func(cond):
00077     cond.acquire()
00078     print('\t' + str(cond))
00079     time.sleep(2)
00080     print('\tchild is notifying')
00081     print('\t' + str(cond))
00082     cond.notify()
00083     cond.release()

def mp_synchronize.event_func (   event)

TEST_EVENT.

Definition at line 171 of file mp_synchronize.py.

00171 
00172 def event_func(event):
00173     print('\t%r is waiting' % multiprocessing.current_process())
00174     event.wait()
00175     print('\t%r has woken up' % multiprocessing.current_process())

TEST_JOIN_TIMEOUT.

Definition at line 150 of file mp_synchronize.py.

00150 
00151 def join_timeout_func():
00152     print('\tchild sleeping')
00153     time.sleep(5.5)
00154     print('\n\tchild terminating')

def mp_synchronize.queue_func (   queue)

TEST_QUEUE.

Definition at line 49 of file mp_synchronize.py.

00049 
00050 def queue_func(queue):
00051     for i in range(30):
00052         time.sleep(0.5 * random.random())
00053         queue.put(i*i)
00054     queue.put('STOP')

def mp_synchronize.semaphore_func (   sema,
  mutex,
  running 
)

TEST_SEMAPHORE.

Definition at line 112 of file mp_synchronize.py.

00112 
00113 def semaphore_func(sema, mutex, running):
00114     sema.acquire()
00115 
00116     mutex.acquire()
00117     running.value += 1
00118     print(running.value, 'tasks are running')
00119     mutex.release()
00120 
00121     random.seed()
00122     time.sleep(random.random()*2)
00123 
00124     mutex.acquire()
00125     running.value -= 1
00126     print('%s has finished' % multiprocessing.current_process())
00127     mutex.release()
00128 
00129     sema.release()

def mp_synchronize.sharedvalues_func (   values,
  arrays,
  shared_values,
  shared_arrays 
)

TEST_SHAREDVALUES.

Definition at line 197 of file mp_synchronize.py.

00197 
00198 def sharedvalues_func(values, arrays, shared_values, shared_arrays):
00199     for i in range(len(values)):
00200         v = values[i][1]
00201         sv = shared_values[i].value
00202         assert v == sv
00203 
00204     for i in range(len(values)):
00205         a = arrays[i][1]
00206         sa = list(shared_arrays[i][:])
00207         assert a == sa
00208 
00209     print('Tests passed')

def mp_synchronize.test (   namespace = multiprocessing)

Definition at line 237 of file mp_synchronize.py.

00237 
00238 def test(namespace=multiprocessing):
00239     global multiprocessing
00240 
00241     multiprocessing = namespace
00242 
00243     for func in [test_value, test_queue, test_condition,
00244                  test_semaphore, test_join_timeout, test_event,
00245                  test_sharedvalues]:
00246 
00247         print('\n\t######## %s\n' % func.__name__)
00248         func()
00249 
00250     ignore = multiprocessing.active_children()      # cleanup any old processes
00251     if hasattr(multiprocessing, '_debug_info'):
00252         info = multiprocessing._debug_info()
00253         if info:
00254             print(info)
00255             raise ValueError('there should be no positive refcounts left')
00256 

Here is the call graph for this function:

Definition at line 84 of file mp_synchronize.py.

00084 
00085 def test_condition():
00086     cond = multiprocessing.Condition()
00087 
00088     p = multiprocessing.Process(target=condition_func, args=(cond,))
00089     print(cond)
00090 
00091     cond.acquire()
00092     print(cond)
00093     cond.acquire()
00094     print(cond)
00095 
00096     p.start()
00097 
00098     print('main is waiting')
00099     cond.wait()
00100     print('main has woken up')
00101 
00102     print(cond)
00103     cond.release()
00104     print(cond)
00105     cond.release()
00106 
00107     p.join()
00108     print(cond)
00109 

Here is the call graph for this function:

Definition at line 176 of file mp_synchronize.py.

00176 
00177 def test_event():
00178     event = multiprocessing.Event()
00179 
00180     processes = [multiprocessing.Process(target=event_func, args=(event,))
00181                  for i in range(5)]
00182 
00183     for p in processes:
00184         p.start()
00185 
00186     print('main is sleeping')
00187     time.sleep(2)
00188 
00189     print('main is setting event')
00190     event.set()
00191 
00192     for p in processes:
00193         p.join()
00194 

Here is the call graph for this function:

Definition at line 155 of file mp_synchronize.py.

00155 
00156 def test_join_timeout():
00157     p = multiprocessing.Process(target=join_timeout_func)
00158     p.start()
00159 
00160     print('waiting for process to finish')
00161 
00162     while 1:
00163         p.join(timeout=1)
00164         if not p.is_alive():
00165             break
00166         print('.', end=' ')
00167         sys.stdout.flush()
00168 

Definition at line 55 of file mp_synchronize.py.

00055 
00056 def test_queue():
00057     q = multiprocessing.Queue()
00058 
00059     p = multiprocessing.Process(target=queue_func, args=(q,))
00060     p.start()
00061 
00062     o = None
00063     while o != 'STOP':
00064         try:
00065             o = q.get(timeout=0.3)
00066             print(o, end=' ')
00067             sys.stdout.flush()
00068         except Empty:
00069             print('TIMEOUT')
00070 
00071     print()
00072 

Here is the call graph for this function:

Definition at line 130 of file mp_synchronize.py.

00130 
00131 def test_semaphore():
00132     sema = multiprocessing.Semaphore(3)
00133     mutex = multiprocessing.RLock()
00134     running = multiprocessing.Value('i', 0)
00135 
00136     processes = [
00137         multiprocessing.Process(target=semaphore_func,
00138                                 args=(sema, mutex, running))
00139         for i in range(10)
00140         ]
00141 
00142     for p in processes:
00143         p.start()
00144 
00145     for p in processes:
00146         p.join()
00147 

Here is the call graph for this function:

Definition at line 210 of file mp_synchronize.py.

00210 
00211 def test_sharedvalues():
00212     values = [
00213         ('i', 10),
00214         ('h', -2),
00215         ('d', 1.25)
00216         ]
00217     arrays = [
00218         ('i', list(range(100))),
00219         ('d', [0.25 * i for i in range(100)]),
00220         ('H', list(range(1000)))
00221         ]
00222 
00223     shared_values = [multiprocessing.Value(id, v) for id, v in values]
00224     shared_arrays = [multiprocessing.Array(id, a) for id, a in arrays]
00225 
00226     p = multiprocessing.Process(
00227         target=sharedvalues_func,
00228         args=(values, arrays, shared_values, shared_arrays)
00229         )
00230     p.start()
00231     p.join()
00232 
00233     assert p.exitcode == 0
00234 

Here is the call graph for this function:

Definition at line 27 of file mp_synchronize.py.

00027 
00028 def test_value():
00029     TASKS = 10
00030     running = multiprocessing.Value('i', TASKS)
00031     mutex = multiprocessing.Lock()
00032 
00033     for i in range(TASKS):
00034         p = multiprocessing.Process(target=value_func, args=(running, mutex))
00035         p.start()
00036 
00037     while running.value > 0:
00038         time.sleep(0.08)
00039         mutex.acquire()
00040         print(running.value, end=' ')
00041         sys.stdout.flush()
00042         mutex.release()
00043 
00044     print()
00045     print('No more running processes')
00046 

Here is the call graph for this function:

def mp_synchronize.value_func (   running,
  mutex 
)

TEST_VALUE.

Definition at line 18 of file mp_synchronize.py.

00018 
00019 def value_func(running, mutex):
00020     random.seed()
00021     time.sleep(random.random()*4)
00022 
00023     mutex.acquire()
00024     print('\n\t\t\t' + str(multiprocessing.current_process()) + ' has finished')
00025     running.value -= 1
00026     mutex.release()


Variable Documentation

tuple mp_synchronize.namespace = multiprocessing

Definition at line 264 of file mp_synchronize.py.