blocking all threads

A

Alexandru Mosoi

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.
 
S

sturlamolden

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top