Worker

Inheritance Diagram

Inheritance diagram of tenpy.tools.thread.Worker

Methods

Worker.__init__([name, max_queue_size, daemon])

Worker.join_tasks()

Block until all worker tasks are finished.

Worker.put_task(fct, *args[, return_dict, ...])

Add a task to be done by the worker.

Worker.run()

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.

name

The name parameter.

Type:

str

tasks

The queue with tasks to be done by the worker.

Type:

queue.Queue

exit

Set by the worker or main thread to indicate that the other thread should terminate.

Type:

threading.Event

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}
run()[source]

Main function for worker thread.

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).

join_tasks()[source]

Block until all worker tasks are finished.