Multi-Process---Error resolution attribute Error:can ' t get attributes ' Long_time_task ' on <module ' __main__ ' (built-in) >
StackOverflow Solutions
This problem seems to be a design feature of multiprocessor. Please refer to https://bugs.python.org/issue25053. Pool cannot always use objects that are not defined in the imported module for some reason. So you have to write the function to a different file and import the module.
Solutions
One file generates an error, the function definition is placed in another py file and then introduced
one.py
From multiprocessing import Pool
import long_time_task
import os
if __name__== ' __main__ ':
print (' Parent process%s. '% Os.getpid ())
p = Pool (4)
for I in range (5):
P.apply_async (Long_time_task, args= (i,)) C8/>print (' Waiting for all subprocesses done ... ')
p.close () p.join ()
print (' All subprocesses done. ')
long_time_task.py
import os, Time, Random def long_time_task (name): Print (' Run task%s (%s) ... '% (name, os.ge Tpid ())) start = Time.time () time.sleep (Random.random () * 3) end = Time.time () print (' Task%s runs%0.2f s Econds. '% (name, (End-start))