Suspend instead of blocking
If the sleep() process is used, it will block
Assuming there are two threads in the process, then these two threads will continue to run.
To make the process hang, consider using psutil
import psutil p = (pid) () #Suspend process() #Recovery process
To prove the effect I wrote a simple process
There are two threads under it, Reader Reader and Writer (simple reader writer problem)
Process: import threading from time import ctime, sleep import ThreadInReadAndWriteProblem import multiprocessing import os class Process(): def __init__(self): .__init__(self) #Manually implement parent class pid = () def run(self): print 'Current running process PID: %s' % #The id of the child thread is the same as the pid of the parent process. It belongs to the same process for i in range(0,5): r = () w = () () () print 'Process blocking' sleep(10) #Total running time 10 secondsReader&Writer import threading from time import ctime, sleep import os mutex = () #mutex lockmutex_readercount = () #Mutex during counting Calculate the number of currently being readreaderCount = 0 number = 0 #The condition does not meet the blocking stateclass Reader(): #reader def __init__(self): .__init__(self) #Inherit the parent class constructor def run(self): global mutex global readerCount #print 'Thread PID: %s ' %() while True: mutex_readercount.acquire() readerCount +=1 if readerCount == 1: print 'Reader process is waiting, number %s' %() () == False # The first one needs to apply mutex_readercount.release() print 'Start read, reader number %s , now time is %s' %(,ctime()) sleep(2) print 'Complete reading, reader number %s , now time is %s' %(,ctime()) mutex_readercount.acquire() readerCount -= 1 if readerCount == 0: #All readers complete print 'The last reader completes reading' () mutex_readercount.release() class Writer(): #Writer def __init__(self): .__init__(self) def run(self): global mutex global writerCount #print 'Thread PID: %s' %() while True: print 'Writer process waiting number: %s' %() () print 'Start writing Number: %s Now time is: %s ' %(,ctime()) sleep(5) print 'End write number: %s Now time is %s' %(,ctime()) ()
Testing procedures
import ThreadInReadAndWriteProblem import SingleProcessSchedulerMultiprocess import psutil import Scheduler from time import ctime, sleep def main(): p = () () sleep(3) stop() print 'Process suspended %s' %ctime() sleep(5) wake() print 'Wake up process %s' %ctime() def stop(pid): print 'Process Pause Process Number %s' %(pid) p = (pid) () def wake(pid): print 'Process Recovery Process Number %s' %(pid) p = (pid) () if __name__ == '__main__': main()
result:
Current running process PID: 3096
Writer process waiting Number: Thread-2
Start writing Number: Thread-2 Now time is: Mon Nov 30 21:12:12 2015
Reader process waiting, number Thread-1
Writer process waiting Number: Thread-4
Process blocking
Writer process waiting Number: Thread-6
Writer process waiting Number: Thread-8
Writer process waiting Number: Thread-10
Process pause Process number 3096
Process suspended Mon Nov 30 21:12:15 2015
Process Recovery Process Number 3096
Wake up process Mon Nov 30 21:12:20 2015
End writing Number: Thread-2 Now time is Mon Nov 30 21:12:20 2015
Writer process waiting Number: Thread-2
Start reading, reader number Thread-1, the current time is Mon Nov 30 21:12:20 2015
Start reading, reader number Thread-3, the current time is Mon Nov 30 21:12:20 2015
Start reading, reader number Thread-5, the current time is Mon Nov 30 21:12:20 2015
Start reading, reader number Thread-7, the current time is Mon Nov 30 21:12:20 2015
Start reading, reader number Thread-9, now time is Mon Nov 30 21:12:20 2015
Complete reading, reader number Thread-1, current time is Mon Nov 30 21:12:22 2015
Complete reading, reader number Thread-3, the current time is Mon Nov 30 21:12:22 2015
Complete reading, reader number Thread-5, current time is Mon Nov 30 21:12:22 2015
Complete reading, reader number Thread-7, the current time is Mon Nov 30 21:12:22 2015
Summarize
The above is the normal way to deal with the pause of Python programs introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!