Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def new_worker(self):
try:
worker = launch_process(
worker_process, self.worker_parameters, self.workers_channel)
self.workers[worker.pid] = worker
except (OSError, EnvironmentError) as error:
raise BrokenProcessPool(error)
def stop_worker(self, worker_id, force=False):
try:
if force:
stop_process(self.workers.pop(worker_id))
else:
with self.workers_channel.lock:
stop_process(self.workers.pop(worker_id))
except ChannelError as error:
raise BrokenProcessPool(error)
except KeyError:
return # worker already expired
context = pool_manager.context
task_queue = context.task_queue
try:
while context.alive:
task = task_queue.get()
if task is not None:
if task.future.cancelled():
task.set_running_or_notify_cancel()
task_queue.task_done()
else:
pool_manager.schedule(task)
else:
task_queue.task_done()
except BrokenProcessPool:
context.state = ERROR
def stop_worker(self, worker_id, force=False):
try:
if force:
stop_process(self.workers.pop(worker_id))
else:
with self.workers_channel.lock:
stop_process(self.workers.pop(worker_id))
except ChannelError as error:
raise BrokenProcessPool(error)
except KeyError:
return # worker already expired
def new_worker(self):
try:
worker = launch_process(
None, worker_process, True, self.worker_parameters, self.workers_channel)
self.workers[worker.pid] = worker
except (OSError, EnvironmentError) as error:
raise BrokenProcessPool(error)
def receive(self, timeout):
try:
if self.pool_channel.poll(timeout):
return self.pool_channel.recv()
else:
return NoMessage()
except (OSError, EnvironmentError, TypeError) as error:
raise BrokenProcessPool(error)
context = pool_manager.context
task_queue = context.task_queue
try:
while context.alive:
task = task_queue.get()
if task is not None:
if task.future.cancelled():
task.future.set_running_or_notify_cancel()
task_queue.task_done()
else:
pool_manager.schedule(task)
else:
task_queue.task_done()
except BrokenProcessPool:
context.state = ERROR
def find_expired_task(self, worker_id):
tasks = tuple(self.task_manager.tasks.values())
running_tasks = tuple(t for t in tasks if t.worker_id != 0)
if running_tasks:
return task_worker_lookup(running_tasks, worker_id)
else:
raise BrokenProcessPool("All workers expired")
def dispatch(self, task):
try:
self.pool_channel.send(WorkerTask(task.id, task.payload))
except (pickle.PicklingError, TypeError) as error:
raise error
except (OSError, EnvironmentError, TypeError) as error:
raise BrokenProcessPool(error)