According to the docs of python's multiprocess the spawning of process needs to be inside of the if __name__ == '__main__':
clause to prevent spawning infinite processes.
My question, is it possible to use multiprocess inside of an import?
Something along those lines of this: let's say I have this py which is the main executed file:
import foo
def main():
foo.run_multiprocess()
if __name__ =='__main__':
main()
and the foo.py file which is imported:
def run_multiprocess(number_to_check):
if number_to_check == 5:
print(number_to_check)
if __name__ == '__main__':
list_to_check = {1,2,3,4,5,6,7}
pool = Pool(processes=4)
pool.map(process_image, list_to_check)
Obviusly this won't work because the code inside the if statment in foo.py won't run. Is there a way to make it work though?
Multiprocessing doesn't have to run within the __main__
block, __main__
block is only used if the file is ran via python filename.py
.
So if you did:
m1.py
:
from multiprocessing import Pool
def f(x):
return x^2
def f2():
p = Pool(5)
p.map(f, [1,2,3,4,5])
m2.py
:
from m1 import f2
if __name__ == '__main__':
f2() # this would run multiprocessing code
and then call python m2.py
, your code would run correctly, with mp.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.