The current file name is: multiprocessing.py
#!/usr/bin/env python3 import os from multiprocessing import Process def child_proc(name): print('child process', name, '[',os.getpid(),']','start running...') if __name__ == '__main__': print('parent process', os.getpid()) for i in range(5): create_childpid = Process(target=child_proc,args=(str(i),)) print('process will start...') create_childpid.start() create_childpid.join() print('process end')
Function:
sice@T:~/lxz$ vim multiprocessing.py sice@T:~/lxz$ chmod +x multiprocessing.py sice@T:~/lxz$ ./multiprocessing.py
Report errors:
Traceback (most recent call last): File "./multiprocessing.py", line 3, in <module> from multiprocessing import Process File "/home/sice/lxz/multiprocessing.py", line 3, in <module> from multiprocessing import Process ImportError: cannot import name 'Process'
The reason for the error is that the file name is the same as the module name
The current file name is: multiprocessing.py Module name: from multiprocessing import process
The solution is as follows:
Step 1. (as long as the file name is different from the module name), change the file name to: aaa.py
Under Linux system
sice@T:~/lxz$ mv multiprocessing.py aaa.py # Rename to aaa.py
Rename in Windows
Step 2. Delete the multiprocessing.pyc file and the pycache folder. All of them have been modified here
#!/usr/bin/env python3 import os from multiprocessing import Process def child_proc(name): print('child process', name, '[',os.getpid(),']','start running...') if __name__ == '__main__': print('parent process', os.getpid()) for i in range(5): create_childpid = Process(target=child_proc,args=(str(i),)) print('process will start...') create_childpid.start() create_childpid.join() print('process end')
Run over aaa.py That's all right.