A utility to create an instance in another process and call its functions seamlessly
Project description
Cross Process Bridge
This package is a utility for creating an instance of a class in a new process and seamlessly calling its functions from the original process
Background
multiprocessing
let's say I have a class that needs to do something that affects the whole process - for example changing the working directory, using setuid, or any other action that cannot be done in a thread without affecting the rest of the process
The module multiprocessing
module allows running a python function in a separate process using
multiprocessing.Process
like this:
process = multiprocessing.Process(target=func_to_run, args=(args, to, send))
process.start()
# do other things
process.join()
why multiprocessing isn't enough
The above example will run the function func_to_run
with the given arguments. the program can then do other things and
wait for the process using process.join()
when it needs to
however, even if the func_to_run
creates an instance of a class, this does not allow calling functions of that class
from the main process - it only runs the func_to_run
and then exits
Cross Process Class
This module allows creating an instance of a class in one process and then seamlessly calling its functions from the main process
Example
let's say i have a class A
which has a method a
which prints the pid of the process in which it is running
class A:
def a(self):
print('a', os.getpid())
and let's further assume, that I didn't just create this class to check whether what i did worked, and say we just really want to run an instance of this class in another process for completely unrelated reasons
The module exports a class CrossProcessBridge
which must be inherited from in order to run in another process
If, for example I want to run an instance of the A
class in a separate process:
I will create a new class, in this case B
which will inherit from both A
and from CrossProcessBridge
important - the new class must inherit first from your class - A
in this example - and then CrossProcessBridge
class B(A, CrossProcessBridge):
pass
after creating an instance of B
I will then call the start()
method from the CrossProcessBridge
class - this will
create another process, and in it create an instance of A
importantly, not of B
! it will create an instance of
the original A
class
I can now call any methods that exist in the A
class on the B
instance I have created and they will be called in the
new process.
when I am done i can call stop()
which will stop the process
Complete example:
import os
from cross_process_bridge import CrossProcessBridge
class A:
def a(self):
print('a', os.getpid())
class B(A, CrossProcessBridge):
pass
if __name__ == '__main__':
pid = os.getpid()
print(pid) # print the pid of the original process
b = B() # create the bridge instance
b.start() # start the process and create an instance in it
b.a() # call the `a()` function - will print a different pid
b.stop() # stop the process
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for cross_process_bridge-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26b7e572063bb6e28ab21c3baeb7f49e39ed2167cce30debff54bc4182acdfea |
|
MD5 | c6bc851bd169654c76112001073d3dea |
|
BLAKE2b-256 | 12616ac3d00cc422b9b358e7df61c9a94662dbbc33c62d3afbdc2d4be779c42d |
Hashes for cross_process_bridge-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76ff7be5022bb204cc8d61a0e5747b0d5a27f29df0652a26aa746160ad5de197 |
|
MD5 | 485bff6bd2d25e48ff8dd4830f1a2c6b |
|
BLAKE2b-256 | 508a91495c7dd28edbe59803bc45372c5d44f18fd75aeb9a85ec6bca06e6e9c3 |