Worker
full name: tenpy.tools.thread.Worker
parent module:
tenpy.tools.thread
type: class
Inheritance Diagram
Methods
|
|
Block until all worker tasks are finished. |
|
|
Add a task to be done by the worker. |
Main function for worker thread. |
- class tenpy.tools.thread.Worker(name='tenpy worker', max_queue_size=0, daemon=None)[source]
Bases:
object
Manager for a worker thread.
Should be used as a context manager in a
with
statement, see the example below.- Parameters:
name (str) – Descriptive name for the worker thread.
max_queue_size (int) – The maxsize for the
queue.Queue
.
- tasks
The queue with tasks to be done by the worker.
- Type:
- exit
Set by the worker or main thread to indicate that the other thread should terminate.
- Type:
Example
>>> def work_to_be_done(a, b): ... # do something ... return a + b >>> with Worker("example") as worker: ... results = {} ... worker.put_task(work_to_be_done, 2, 2, return_dict=results, return_key="2+2") ... # go do something else, then add another task ... worker.put_task(work_to_be_done, a=3, b=4, return_dict=results, return_key="3+4") ... # "2+2" might be in results, but doesn't have to be yet ... worker.join_tasks() # block/wait until all the tasks have been done ... assert "3+4" in results # now we can be sure that we got all results >>> results {'2+2': 4, '3+4': 7}
- put_task(fct, *args, return_dict=None, return_key=None, **kwargs)[source]
Add a task to be done by the worker.
The worker will eventually do:
res = fct(*args, **kwargs) if return_dict is not None: return_dict[return_key] = res
It is unclear at which exact moment this happens, but after
join_tasks()
was called, it is guaranteed to be done (or an exception was raised that the worker died).