ori.concurrency

Utilities for concurrency of Python functions with multithreading or multiprocessing.

ori.concurrency.run_in_background(func, *, executor_class, max_workers=None)

Runs the function in a background thread or process, depending on the executor that you provided.

Most users will want to use ori.concurrency.run_in_background_thread() or ori.concurrency.run_in_background_process() instead.

Parameters
  • func (Callable) – The function to run in the background.

  • executor_class (type) – A concurrent.futures.Executor subclass that the function will be submitted to. Two popular choices are concurrent.futures.ThreadPoolExecutor or concurrent.futures.ProcessPoolExecutor, but you can pass your own subclass if if it follows the same API.

  • max_workers (Optional[int]) – Specify this to set the maximum number of parallel threads or processes to run at once. If this is set to None, then the executor’s default maximum number of workers are used.

Returns

This function returns a wrapper of the func function that you passed. The wrapper takes the exact same arguments, but instead returns a concurrent.futures.Future object. To wait for the return value of func, call .result() on the future that this function returns.

Raises

OriValidationError – When you provide a value for func that is not a callable function.

ori.concurrency.run_in_background_process(func, *, max_workers=None)

Runs the function in a background process.

Parameters
  • func (Callable) – The function to run in the background.

  • max_workers (Optional[int]) – Specify this to set the maximum number of parallel threads or processes to run at once. If this is set to None, then the executor’s default maximum number of workers are used.

Returns

This function returns a wrapper of the func function that you passed. The wrapper takes the exact same arguments, but instead returns a concurrent.futures.Future object. To wait for the return value of func, call .result() on the future that this function returns.

Raises

OriValidationError – When you provide a value for func that is not a callable function.

ori.concurrency.run_in_background_thread(func, *, max_workers=None)

Runs the function in a background thread.

Parameters
  • func (Callable) – The function to run in the background.

  • max_workers (Optional[int]) – Specify this to set the maximum number of parallel threads or processes to run at once. If this is set to None, then the executor’s default maximum number of workers are used.

Returns

This function returns a wrapper of the func function that you passed. The wrapper takes the exact same arguments, but instead returns a concurrent.futures.Future object. To wait for the return value of func, call .result() on the future that this function returns.

Raises

OriValidationError – When you provide a value for func that is not a callable function.