mirror of
https://github.com/wassname/baukit.git
synced 2026-06-27 17:14:53 +08:00
Support keyword arguments on work.
This commit is contained in:
@@ -58,14 +58,16 @@ class WorkerBase(Process):
|
|||||||
# Do the work until None is dequeued
|
# Do the work until None is dequeued
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
work_batch = self.queue.get()
|
work = self.queue.get()
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
print('Exiting...')
|
print('Exiting...')
|
||||||
break
|
break
|
||||||
if work_batch is None:
|
if work is None:
|
||||||
self.queue.put(None) # for another worker
|
self.queue.put(None) # for another worker
|
||||||
return
|
return
|
||||||
self.work(*work_batch)
|
else:
|
||||||
|
work_batch, work_kw = work
|
||||||
|
self.work(*work_batch, **work_kw)
|
||||||
|
|
||||||
def setup(self, **initargs):
|
def setup(self, **initargs):
|
||||||
'''
|
'''
|
||||||
@@ -74,7 +76,7 @@ class WorkerBase(Process):
|
|||||||
'''
|
'''
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def work(self, *args):
|
def work(self, *args, **kwargs):
|
||||||
'''
|
'''
|
||||||
Override this method for one-time initialization.
|
Override this method for one-time initialization.
|
||||||
Args are passed from WorkerPool.add() arguments.
|
Args are passed from WorkerPool.add() arguments.
|
||||||
@@ -113,16 +115,16 @@ class WorkerPool(object):
|
|||||||
# The main process should handle ctrl-C. Restore this now.
|
# The main process should handle ctrl-C. Restore this now.
|
||||||
signal.signal(signal.SIGINT, original_sigint_handler)
|
signal.signal(signal.SIGINT, original_sigint_handler)
|
||||||
|
|
||||||
def add(self, *work_batch):
|
def add(self, *work_batch, **work_kw):
|
||||||
if self.queue is None:
|
if self.queue is None:
|
||||||
if hasattr(self, 'worker'):
|
if hasattr(self, 'worker'):
|
||||||
self.worker.work(*work_batch)
|
self.worker.work(*work_batch, **work_kw)
|
||||||
else:
|
else:
|
||||||
print('WorkerPool shutting down.', file=sys.stderr)
|
print('WorkerPool shutting down.', file=sys.stderr)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
# The queue can block if the work is so slow it gets full.
|
# The queue can block if the work is so slow it gets full.
|
||||||
self.queue.put(work_batch)
|
self.queue.put([work_batch, work_kw])
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
# Handle ctrl-C if done while waiting for the queue.
|
# Handle ctrl-C if done while waiting for the queue.
|
||||||
self.early_terminate()
|
self.early_terminate()
|
||||||
|
|||||||
Reference in New Issue
Block a user