1414import sys
1515
1616import gevent .pool
17- import multiprocessing as mp
17+ import multiprocess as mp # used by pathos.pools.ProcessPool
1818import numpy as np
1919import pathos .pools
2020from tqdm import tqdm
@@ -472,15 +472,15 @@ def execute(self, progress=True, parallel=None, total=None):
472472
473473 # Don't fork, spawn entirely new processes. This
474474 # avoids accidental deadlocks.
475- mp .set_start_method ("spawn" , force = True )
475+ spawn_ctx = mp .get_context ("spawn" )
476476
477477 with tqdm (total = total , desc = "Tasks" , disable = (not progress )) as pbar :
478478 if self .parallel == 1 :
479479 while self .queue :
480480 _task_execute (self .queue .pop (0 ))
481481 pbar .update ()
482482 else :
483- with pathos .pools .ProcessPool (self .parallel ) as executor :
483+ with pathos .pools .ProcessPool (self .parallel , context = spawn_ctx ) as executor :
484484 for _ in executor .imap (_task_execute , self .queue ):
485485 pbar .update ()
486486
@@ -513,8 +513,6 @@ def soloprocess_upload(QueueClass, queue_name, tasks):
513513 tq = QueueClass (queue_name , progress = False )
514514 return tq .insert (tasks , skip_insert_counter = True )
515515
516- error_queue = mp .Queue ()
517-
518516def multiprocess_upload (QueueClass , queue_name , tasks , parallel = True , total = None ):
519517 if parallel is True :
520518 parallel = mp .cpu_count ()
@@ -526,11 +524,10 @@ def multiprocess_upload(QueueClass, queue_name, tasks, parallel=True, total=None
526524
527525 def capturing_soloprocess_upload (* args , ** kwargs ):
528526 try :
529- return soloprocess_upload (* args , ** kwargs )
527+ return ( soloprocess_upload (* args , ** kwargs ), None )
530528 except Exception as err :
531529 print (err )
532- error_queue .put (err )
533- return 0
530+ return (0 , err )
534531
535532 uploadfn = partial (
536533 capturing_soloprocess_upload , QueueClass , queue_name
@@ -569,12 +566,17 @@ def capturing_soloprocess_upload(*args, **kwargs):
569566
570567 # Don't fork, spawn entirely new processes. This
571568 # avoids accidental deadlocks.
572- mp .set_start_method ("spawn" , force = True )
569+ spawn_ctx = mp .get_context ("spawn" )
573570
574571 ct = 0
572+ errors = []
575573 with tqdm (desc = "Upload" , total = total ) as pbar :
576- with pathos .pools .ProcessPool (parallel ) as pool :
577- for num_inserted in pool .imap (uploadfn , sip (tasks , block_size )):
574+ with pathos .pools .ProcessPool (parallel , context = spawn_ctx ) as pool :
575+ for num_inserted , err in pool .imap (uploadfn , sip (tasks , block_size )):
576+ if err is not None :
577+ if not isinstance (err , StopIteration ):
578+ errors .append (err )
579+ continue
578580 pbar .update (num_inserted )
579581 ct += num_inserted
580582
@@ -584,14 +586,8 @@ def capturing_soloprocess_upload(*args, **kwargs):
584586 os .environ ["no_proxy" ] = no_proxy
585587 # task.__class__.__module__ = cls_module
586588
587- if not error_queue .empty ():
588- errors = []
589- while not error_queue .empty ():
590- err = error_queue .get ()
591- if err is not StopIteration :
592- errors .append (err )
593- if len (errors ):
594- raise Exception (errors )
589+ if errors :
590+ raise Exception (errors )
595591
596592 return ct
597593
0 commit comments