blocking all threads

sturlamolden sturlamolden at
Mon Sep 29 19:35:58 CEST 2008

On Sep 29, 4:56 pm, Alexandru  Mosoi <brtz... at> 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)
  #include <unistd.h>
  #define __sleep(ms) usleep((useconds_t)ms)

void sleep(int ms)

Save this in a file called "gilsleep.c" and compile it as a DLL
(gilslepp.dll in Windows, 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