timeflux.helpers.background


Run a Python method in a lightweight background process.

Example

from timeflux.helpers.background import Task
from my.module import MyClass

task = Task(MyClass(), 'my_method', my_arg=42).start()
while not task.done:
    status = task.status()
print(status)

background

class timeflux.helpers.background.Runner[source]

Background base class. Provides common methods.

Warning

Do not use directly!

class timeflux.helpers.background.Task(instance, method, *args, **kwargs)[source]

Bases: Runner

Background task.

Launch a 0MQ PAIR server, start a client and dispatch the task.

Variables:

done (bool) – Indicates if the task is complete.

Parameters:
  • instance (object) – A picklable class instance.

  • method (string) – The method name to call from the instance.

  • *args – Arbitrary variable arguments to be passed to the method.

  • **kwargs – Arbitrary keyword arguments to be passed to the method.

start()[source]

Run the task.

stop()[source]

Terminate the task.

status()[source]

Get the task status.

Returns:

None if the task is not complete or a dict containing the following keys.

  • success: A boolean indicating if the task ran successfully.

  • instance: The (possibly modified) instance.

  • result: The result of the method call, if success is True.

  • exception: The exception, if success is False.

  • traceback: The traceback, if success is False.

  • time: The time it took to run the task.

class timeflux.helpers.background.Worker(port)[source]

Bases: Runner

Background worker. Connects to the server and executes the task.

Warning

Do not use directly!

execute()[source]

Get the task from the socket and run it.

timeflux.helpers.background.port