Python multiprocessing.Process that does not need to import __main__.
Project description
Python multiprocessing.Process that does not need to import __main__.
Normally python multiprocessing using the __main__ module to create and initiallize the process. The LightProcess allows you to change what module is used to create and initialize the process. By default the LightProcess uses the module that calls LightProcess.start()
Example
# readme_module.py
import light_process as lp
import multiprocessing as mp
def run():
print('readme_module')
def run_light_process():
proc = lp.LightProcess(target=run)
proc.start()
proc.join()
def run_multiprocessing():
proc = mp.Process(target=run)
proc.start()
proc.join()
Multiprocessing works by using the __main__ module to import and seutp the environment for multiprocess. Using the multiprocessing.Process requires you to put your multiprocessing code in “if __name__ == ‘__main__’:”. This can be shown in the following example.
# readme_main_multiprocessing.py
import readme_module
print('__main__')
if __name__ == '__main__':
print("if __name__ == '__main__':")
readme_module.run_multiprocessing()
# Output:
# __main__
# if __name__ == '__main__':
# __main__
# readme_module
#
# Note:
# '__main__' and 'readme_module' will be printed
# '__main__' will be printed twice, because this module is imported in the other process
The main takeaway form this is that the main module is imported twice and requires you to run you multiprocessing code in the “if __name__ == ‘__main__’:” block.
LightProcess was made to remove __main__ from being imported. I have large applications that use multiprocessing. I do not need to load the entire enviroment for the process. I simply need one small module to be imported and run. The example below shows how you can run the processes in __main__ and how it doesn’t import __main__ in the new process.
# readme_main_light_process.py
import readme_module
print('__main__')
# Do not need "if __name__ == '__main__':"
readme_module.run_light_process()
# Output:
# __main__
# readme_module
#
# Note:
# '__main__' and 'readme_module' will be printed
# '__main__' will only be printed once
Project details
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
File details
Details for the file light_process-0.0.7.tar.gz
.
File metadata
- Download URL: light_process-0.0.7.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5df626a9e2eba406cd62e3c4ff5d2e9d263548c66477796f76b8f40ed68acbc |
|
MD5 | 99c871ea348b07eb1dc6295781d197c2 |
|
BLAKE2b-256 | a906e73eeaabe2b0ae03207cdd99e62f7f084f1aaba77f8c36a09056678477fd |
File details
Details for the file light_process-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: light_process-0.0.7-py3-none-any.whl
- Upload date:
- Size: 18.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b33f9d762d5f90496c3c5dc8599a783eb55808b946c879c9a23d1ea480a650be |
|
MD5 | 08cd64efc7cbb065cae9a3d14aa48e26 |
|
BLAKE2b-256 | c2094a94bbb3fa0ec05eb8a4f67c1f1a41425bd95e2497add86c3a1f004c784f |