blocking all threads

sturlamolden sturlamolden at yahoo.no
Mon Sep 29 19:35:58 CEST 2008


On Sep 29, 4:56 pm, Alexandru  Mosoi <brtz... at gmail.com> wrote:

> how can I block all threads for a specific amount of time? (i need to
> sleep whole process for testing purposes). i thought of accessing GIL
> and sleep for some amount of time, but I don't know how to do this and
> whether GIL is recursive.


You could do this in C by sleeping while holding the GIL:

#ifdef WIN32
  #include <Windows.h>
  #define __sleep(ms) Sleep((DWORD)ms)
#else
  #include <unistd.h>
  #define __sleep(ms) usleep((useconds_t)ms)
#endif

__declspec(dllexport)
void sleep(int ms)
{
   __sleep(ms);
}


Save this in a file called "gilsleep.c" and compile it as a DLL
(gilslepp.dll in Windows, gilsleep.so in Linux). Then in Python:

import ctypes
sleep = ctypes.pydll.gilsleep.sleep
sleep.argtypes = (ctypes.c_int,)
sleep.restype = None

sleep(500) # blocks all threads for 500 ms
























More information about the Python-list mailing list