1.7.3. Executors¶
Executors control, if a connector is executed sequentially, in a parallel thread or even a separate process.
Use the connectors.executor() function to instantiate an executor.
1.7.3.1. Base class¶
-
class
connectors._common._executors.Executor¶ a base class for managing the event loop and the execution in threads or processes.
-
get_event_loop()¶ Returns the currently active event loop. This can be None, if no coroutine, task or future is currently being processed.
Returns: the event loop or None
-
run_coroutine(coro)¶ Takes a coroutine and runs it in a newly created event loop.
Parameters: coro – the coroutine Returns: the return value of the coroutine
-
run_coroutines(coros)¶ Takes multiple coroutines and runs them in a newly created event loop.
Parameters: coros – a sequence of coroutines
-
run_method(parallelization, method, instance, *args, **kwargs)¶ Abstract method, whose overrides shall execute the given method. The parallelization shall be implemented in this method.
Parameters: - parallelization – a flag of
connectors.Parallelization, that specifies how the given method can be parallelized - method – the unbound method, that shall be executed
- instance – the instance of which the method shall be executed
- *args,**kwargs – arguments for the method
Returns: the return value of the method
- parallelization – a flag of
-
run_until_complete(future)¶ Takes a future or a task and runs it in a newly created event loop. This is a wrapper for the event loop’s
run_until_complete()method.Parameters: future – the future or the task Returns: the return value of the execution
-
1.7.3.2. Executor classes¶
-
class
connectors._common._executors.SequentialExecutor¶ An executor class, that executes everything sequentially.
-
get_event_loop()¶ Returns the currently active event loop. This can be None, if no coroutine, task or future is currently being processed.
Returns: the event loop or None
-
run_coroutine(coro)¶ Takes a coroutine and runs it in a newly created event loop.
Parameters: coro – the coroutine Returns: the return value of the coroutine
-
run_coroutines(coros)¶ Takes multiple coroutines and runs them in a newly created event loop.
Parameters: coros – a sequence of coroutines
-
run_method(parallelization, method, instance, *args, **kwargs)¶ Executes the given method sequentially.
Parameters: - parallelization – a flag of
connectors.Parallelization, that specifies how the given method can be parallelized - method – the unbound method, that shall be executed
- instance – the instance of which the method shall be executed
- *args,**kwargs – arguments for the method
Returns: the return value of the method
- parallelization – a flag of
-
run_until_complete(future)¶ Takes a future or a task and runs it in a newly created event loop. This is a wrapper for the event loop’s
run_until_complete()method.Parameters: future – the future or the task Returns: the return value of the execution
-
-
class
connectors._common._executors.ThreadingExecutor(number_of_threads)¶ An executor class, that can parallelize computations with threads.
Parameters: number_of_threads – the maximum number of threads, that shall be created, or None to determine this number automatically. -
get_event_loop()¶ Returns the currently active event loop. This can be None, if no coroutine, task or future is currently being processed.
Returns: the event loop or None
-
run_coroutine(coro)¶ Takes a coroutine and runs it in a newly created event loop.
Parameters: coro – the coroutine Returns: the return value of the coroutine
-
run_coroutines(coros)¶ Takes multiple coroutines and runs them in a newly created event loop.
Parameters: coros – a sequence of coroutines
-
run_method(parallelization, method, instance, *args, **kwargs)¶ Executes the given method in a thread if possible and falls back to sequential execution if not.
Parameters: - parallelization – a flag of
connectors.Parallelization, that specifies how the given method can be parallelized - method – the unbound method, that shall be executed
- instance – the instance of which the method shall be executed
- *args,**kwargs – arguments for the method
Returns: the return value of the method
- parallelization – a flag of
-
run_until_complete(future)¶ Takes a future or a task and runs it in a newly created event loop. This is a wrapper for the event loop’s
run_until_complete()method.Parameters: future – the future or the task Returns: the return value of the execution
-
-
class
connectors._common._executors.MultiprocessingExecutor(number_of_processes)¶ An executor class, that can parallelize computations with processes.
Parameters: number_of_processes – the maximum number of processes, that shall be created, or None to determine this number automatically (in this case, the number of CPU cores will be taken). -
get_event_loop()¶ Returns the currently active event loop. This can be None, if no coroutine, task or future is currently being processed.
Returns: the event loop or None
-
run_coroutine(coro)¶ Takes a coroutine and runs it in a newly created event loop.
Parameters: coro – the coroutine Returns: the return value of the coroutine
-
run_coroutines(coros)¶ Takes multiple coroutines and runs them in a newly created event loop.
Parameters: coros – a sequence of coroutines
-
run_method(parallelization, method, instance, *args, **kwargs)¶ Executes the given method in a process if possible and falls back to sequential execution if not.
Parameters: - parallelization – a flag of
connectors.Parallelization, that specifies how the given method can be parallelized - method – the unbound method, that shall be executed
- instance – the instance of which the method shall be executed
- *args,**kwargs – arguments for the method
Returns: the return value of the method
- parallelization – a flag of
-
run_until_complete(future)¶ Takes a future or a task and runs it in a newly created event loop. This is a wrapper for the event loop’s
run_until_complete()method.Parameters: future – the future or the task Returns: the return value of the execution
-
-
class
connectors._common._executors.ThreadingMultiprocessingExecutor(number_of_threads, number_of_processes)¶ An executor class, that can parallelize computations with both threads and processes.
Parameters: - number_of_threads – the maximum number of threads, that shall be created, or None to determine this number automatically.
- number_of_processes – the maximum number of processes, that shall be created, or None to determine this number automatically (in this case, the number of CPU cores will be taken).
-
get_event_loop()¶ Returns the currently active event loop. This can be None, if no coroutine, task or future is currently being processed.
Returns: the event loop or None
-
run_coroutine(coro)¶ Takes a coroutine and runs it in a newly created event loop.
Parameters: coro – the coroutine Returns: the return value of the coroutine
-
run_coroutines(coros)¶ Takes multiple coroutines and runs them in a newly created event loop.
Parameters: coros – a sequence of coroutines
-
run_method(parallelization, method, instance, *args, **kwargs)¶ Executes the given method in a process if possible and falls back to threaded and then sequential execution if not.
Parameters: - parallelization – a flag of
connectors.Parallelization, that specifies how the given method can be parallelized - method – the unbound method, that shall be executed
- instance – the instance of which the method shall be executed
- *args,**kwargs – arguments for the method
Returns: the return value of the method
- parallelization – a flag of
-
run_until_complete(future)¶ Takes a future or a task and runs it in a newly created event loop. This is a wrapper for the event loop’s
run_until_complete()method.Parameters: future – the future or the task Returns: the return value of the execution