SmartProcess¤
Extension to multiprocessing.Process
with multiple memory-management
options, the ability to return results from processes, and more.
This is a relatively low-level part of SmartMultiprocessing. Average users are
probably more interested in more abstract parts of this library, like
SmartPool
and SmartQueue
, which can initialise as many SmartProcess
es as you'd
like and include GUI options.
The API of SmartProcess
matches that of multiprocessing.Process
, except with
some notable additions like:
SmartProcess.resource_usage()
- get the process' current CPU and memory usage.
SmartProcess.is_finished()
- boolean for if process is done.
SmartProcess.get_result()
- get the result from target
, if process finished.
SmartProcess.get_children()
- get all child processes of this process.
__init__(group=None, target=None, name=None, args=(), kwargs={}, daemon=None, fetch_result=False)
¤
Create a new SmartProcess object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group |
None
|
Argument that must always be None. It exists for compatibility with the threading.Thread API. |
None
|
target |
Callable
|
Target function to run on. |
None
|
name |
str
|
Name of process to pass to multiprocessing.Process. |
None
|
args |
Iterable[Any]
|
Arguments to pass to |
()
|
kwargs |
Mappable[str, Any]
|
Keyword arguments to pass to |
{}
|
daemon |
None or bool
|
Whether or not to run as a daemon (background process). Passed to multiprocessing.Process. TODO: not sure if this can actually be supported... |
None
|
fetch_result |
bool
|
Whether or not to try and fetch a result in-memory for |
False
|
Notes
CPU and memory usage tracking is done using psutil
. Support on systems that
aren't Linux, Mac, or Windows may be limited.
Examples:
Start a process that prints to the console in the background:
>>> p = smartmultiprocessing.SmartProcess(target=lambda: print("Hello World"))
>>> p.start()
Hello World
We can check if the process is finished:
>>> p.is_finished()
True
or get its exitcode:
>>> p.get_exitcode()
0
get_children(recursive=True)
¤
Gets the children of the process. TODO: document params
memory_usage(children=False)
¤
Returns current memory usage of the process.. TODO: document params
cpu_usage(interval=None, children=False)
¤
Returns current CPU usage of the process.. TODO: document params
resource_usage(interval=None, children=False)
¤
get_result(join=False, timeout=None, pipe_timeout=1.0)
¤
Attempts to fetch a result for the process. TODO: document params
get_exitcode()
¤
Returns the exitcode of the process, which is only set if it has finished.
run()
¤
Runs the target of the process.
Not recommended: use SmartProcess.start()
instead.
start()
¤
Starts the process.
join(timeout=None)
¤
Joins the thread of the process (assuming it has been started already) and blocks until completion.
is_alive()
¤
Boolean of whether or not the process is currently running.
is_finished()
¤
Boolean of whether or not the process has finished.
terminate(children=True)
¤
Sends a SIGTERM and terminates the process. TODO: document params
kill(children=True)
¤
Sends a SIGKILL and kills the process. TODO: document params
close()
¤
Closes all resources occupied by the process.