python - How to force execution of a different thread -


I have a main thread that some CPU operates intensive. Thread is locked for all its calculations.

Then there are some other threads that require a single lock for some time.

I sometimes allow the main main thread to execute it without slowing down other threads if there are no other threads?

A periodic

  lock.release () time.sleep (x) lock.acquire ()  

For some x Other threads have to go through the control, but slow the main thread if there is no other thread.

On the other hand, without calling sleep () GIL seems to be interfering here. Since the main thread is GIL, when it executes release () , then the other thread is not immediately able to return from the acquire () , therefore, the main thread is its own Continuation with the acquisition () method and other threads are never locked until the GIL switch is equally with me to release my own lock.

What is the proper solution for this problem? What is the way to force GIL release? Actually I need some magic piece which ensures that the second thread is always locked in the following test script first:

  import time class T1 (Threading. Thread) Import: def run (Self): print "lock the second thread for lock" lock.acquire () print "lock the second thread" lock.release () lock = threading.lock () lock.acquire () t = t1 () t Start () Time.sleep (1) lock.release () time.sleep (0) # It works very often, but not lock all the time .acquire () print "head Thread is locked "lock.release ()  

Not only guarantee of issuing GIL That will get the chance to run the other thread.

In Unix, you really want to call schedule_yield () . There is no interface of that function in the Python standard library; It would be easy to add one with an original module

Sleep (0) and choose () are sometimes used for the same purpose, although it is always based on the system scheduler Not only does Windows, you want to sleep (0) use time.sleep (0) for both of them.


Comments