1from multiprocessing import Process
2import os
3
4def info(title):
5 print(title)
6 print('module name:', __name__)
7 print('parent process:', os.getppid())
8 print('process id:', os.getpid())
9
10def f(name):
11 info('function f')
12 print('hello', name)
13
14if __name__ == '__main__':
15 info('main line')
16 p = Process(target=f, args=('bob',))
17 p.start()
18 p.join()
1from multiprocessing import Process
2
3def f(name):
4 print('hello', name)
5
6if __name__ == '__main__':
7 p = Process(target=f, args=('bob',))
8 p.start()
9 p.join()
1import multiprocessing as mp
2
3def foo(q):
4 q.put('hello')
5
6if __name__ == '__main__':
7 mp.set_start_method('spawn')
8 q = mp.Queue()
9 p = mp.Process(target=foo, args=(q,))
10 p.start()
11 print(q.get())
12 p.join()
1import multiprocessing as mp
2
3def foo(q):
4 q.put('hello')
5
6if __name__ == '__main__':
7 ctx = mp.get_context('spawn')
8 q = ctx.Queue()
9 p = ctx.Process(target=foo, args=(q,))
10 p.start()
11 print(q.get())
12 p.join()