![]() ![]() Semaphore ( 3 ) jobs = for j in jobs : j. makeInactive ( name ) if _name_ = '_main_' : pool = ActivePool () s = multiprocessing. makeActive ( name ) print 'Now running: %s ' % str ( pool ) time. active ) def worker ( s, pool ): name = multiprocessing. remove ( name ) def _str_ ( self ): with self. A Python Queue is quite slow the correct object to use as a fast queue data structure is the deque object from collections. append ( name ) def makeInactive ( self, name ): with self. Lock () def makeActive ( self, name ): with self. Import random import multiprocessing import time class ActivePool ( object ): def _init_ ( self ): super ( ActivePool, self ). Parallel, but only after the first stage is done. In this example, two process run the second stage of a job in Process ( name = 's1', target = stage_1, args = ( condition ,)) s2_clients = ' % i, target = stage_2, args = ( condition ,)) for i in range ( 1, 3 ) ] for c in s2_clients : c. wait () print ' %s running' % name if _name_ = '_main_' : condition = multiprocessing. name print 'Starting', name with cond : cond. notify_all () def stage_2 ( cond ): """wait for the condition telling us stage_1 is done""" name = multiprocessing. name print 'Starting', name with cond : print ' %s done and ready for stage 2' % name cond. Import multiprocessing import time def stage_1 ( cond ): """perform first stage of work, then notify stage_2 to continue""" name = multiprocessing. Parallelized there is no guarantee about the order they will be get () print 'Result:', result num_jobs -= 1Īlthough the jobs enter the queue in order, since their execution is join () # Start printing results while num_jobs : result = results. put ( None ) # Wait for all of the tasks to finish tasks. put ( Task ( i, i )) # Add a poison pill for each consumer for i in xrange ( num_consumers ): tasks. start () # Enqueue jobs num_jobs = 10 for i in xrange ( num_jobs ): tasks. cpu_count () * 2 print 'Creating %d consumers' % num_consumers consumers = for w in consumers : w. Queue () # Start consumers num_consumers = multiprocessing. JoinableQueue () results = multiprocessing. b ) if _name_ = '_main_' : # Establish communication queues tasks = multiprocessing. b ) def _str_ ( self ): return ' %s * %s ' % ( self. sleep ( 0.1 ) # pretend to take some time to do the work return ' %s * %s = %s ' % ( self. put ( answer ) return class Task ( object ): def _init_ ( self, a, b ): self. queue.py and testall.py are in the same directory. Here is the git repo where queue.py becomes a problem in python 3. So I wondered if there is way to keep the name in python 3. task_done () break print ' %s : %s ' % ( proc_name, next_task ) answer = next_task () self. However, for clarity, I would like to show my audience that Queue.java -> queue.py. get () if next_task is None : # Poison pill means shutdown print ' %s : Exiting' % proc_name self. result_queue = result_queue def run ( self ): proc_name = self. ![]() Process ): def _init_ ( self, task_queue, result_queue ): multiprocessing. Import multiprocessing import time class Consumer ( multiprocessing. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |