python3 进程调用

code
import multiprocessing
import time
def foo():
    print ('Starting function')
time.sleep(0.1)
print ('Finished function')
 
 
if __name__ == '__main__':
    p = multiprocessing.Process(target=foo)
    print ('Process before execution:', p, p.is_alive())
    p.start()
    print ('Process running:', p, p.is_alive())
    p.terminate()
    print ('Process terminated:', p, p.is_alive())
    p.join()
    print ('Process joined:', p, p.is_alive())
    print ('Process exit code:', p.exitcode)
outputs
macname@MacdeMBP files % python3 test.py
Finished function
Process before execution: <Process(Process-1, initial)> False
Process running: <Process(Process-1, started)> True
Process terminated: <Process(Process-1, started)> True
Process joined: <Process(Process-1, stopped[SIGTERM])> False
Process exit code: -15
macname@MacdeMBP files % 
 
 
 
 
 
 
 
 
 
 
 
 

原文地址:https://www.cnblogs.com/sea-stream/p/14181802.html