Python multiprocess entry point could be controlled?
I've injected run_entry
function on entry.py
.
But why new process importing main.py
again?
instead of starting with run_entry func on entry.py by direct importing entry.py
I've tested it on Windows 10 (Python 3.9.13).
I want to use inter-process communication by shared memory Queue.
So, I don't want to use a subprocess.
but Is there any method to use Queue with subprocess?
main.py
from entry import call_multi_process
print(f'i am IMPORTED MAIN : {__name__} <-- WHY THIS LOADED MULTIPLE TIMES?')
def main():
call_multi_process()
if __name__ == '__main__':
print('i am the GENESIS OF MAIN')
main()
print('i am the ENDGAME OF MAIN')
entry.py
import time
from concurrent.futures import ProcessPoolExecutor
import psutil
print(f'i am IMPORTED AS MULTIPROCESS ENTRY OWNER : {__name__}')
def run_entry(num: int):
name = psutil.Process().name()
print(f'I AM MULTIPROCESS BODY / {__name__} / {num}:{name} {psutil.Process().pid}')
time.sleep(1)
def call_multi_process():
# I WANT PURE NEWLY CREATED PROCESS ONLY WITH IMPORTING THIS PY FILE WITHOUT MAIN
with ProcessPoolExecutor(initargs=('name', 'test')) as pool:
for i in range(3):
pool.submit(run_entry, i)
# time.sleep(1)
Output
i am IMPORTED AS MULTIPROCESS ENTRY OWNER : pyutil.tests.new_process_entry
i am IMPORTED MAIN : _main_ <-- WHY THIS LOADED MULTIPLE TIMES?
i am the GENESIS OF MAIN
i am IMPORTED AS MULTIPROCESS ENTRY OWNER : pyutil.tests.new_process_entry
i am IMPORTED MAIN : _mp_main_ <-- WHY THIS LOADED MULTIPLE TIMES?
I AM MULTIPROCESS BODY / pyutil.tests.new_process_entry / 0:python.exe 30596
i am IMPORTED AS MULTIPROCESS ENTRY OWNER : pyutil.tests.new_process_entry
i am IMPORTED MAIN : _mp_main_ <-- WHY THIS LOADED MULTIPLE TIMES?
I AM MULTIPROCESS BODY / pyutil.tests.new_process_entry / 1:python.exe 28200
i am IMPORTED AS MULTIPROCESS ENTRY OWNER : pyutil.tests.new_process_entry
i am IMPORTED MAIN : _mp_main_ <-- WHY THIS LOADED MULTIPLE TIMES?
I AM MULTIPROCESS BODY / pyutil.tests.new_process_entry / 2:python.exe 6336
i am the ENDGAME OF MAIN