Skip to content

Instantly share code, notes, and snippets.

@siuoly
Last active March 16, 2022 14:10
Show Gist options
  • Save siuoly/d6f324f15aba5d27eecb8482fbd15569 to your computer and use it in GitHub Desktop.
Save siuoly/d6f324f15aba5d27eecb8482fbd15569 to your computer and use it in GitHub Desktop.
python process state change example
#!/bin/python
# show process start
import multiprocessing
from multiprocessing import Process
import time
### Test : join(), start(), is_alive, close()
p = Process( target=time.sleep , args=(10,))
print( p ) # show <process name>, <pid> <parent> state=initial
print( p.is_alive() )# False, not start
### Started process
p.start()
print( p ) # ... state = started
# p.start() # again-- AssertionError: cannot start a process twice
### join
p.join()
print( p ) # state:stopped , with exitcode imformation
# p.join() # again-- nothing happen
### close
p.close()
print( p ) # state: close, no pid,exitcode information
# p.close() # nothing happen
# p.join() # again-- ValueError: process object is closed
### Terminte,
# p.start()
# p.terminate()
# print( p ) # state: stopped
### run
p = Process( target=print , args=(10,))
p.run() # run the process in current proecess, and the process will do nothing
# p.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment