-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
C++ Embedded 'time.sleep()' is not working on Windows host due to 'Py_InitializeEx(0)' #85852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi, time.sleep() function is not working on Windows host if I use C API Py_InitializeEx(0) function. Issue could be reproduce with following simple example. int main()
{
Py_InitializeEx(0);
PyRun_SimpleString("import time \n"
"start = time.time() \n"
"time.sleep(4) \n"
"print('elapsed: %d' % (time.time() - start))");
} output: elapsed: 0 Note, issue is not reproduce on Linux host. |
The SIGINT event gets created when the _signal extension module is imported. Until then, _PyOS_SigintEvent() returns NULL. But currently all code that calls _PyOS_SigintEvent() assumes it returns a valid handle. This has to be fixed to support Py_InitializeEx(0). For example, pysleep could call WinAPI Sleep on the main thread if the interrupt event isn't configured: ul_millis = (unsigned long)millisecs;
hInterruptEvent = _PyOS_SigintEvent(); if (ul_millis == 0 || !hInterruptEvent || !_PyOS_IsMainThread()) {
Py_BEGIN_ALLOW_THREADS
Sleep(ul_millis);
Py_END_ALLOW_THREADS
break;
} |
Py_InitializeEx() should be modified to always initialize sigint_event, even if install_sigs is equal to zero. Maybe the variable should be moved somewhere else. Maybe in the time module? |
By the way, it would be nice to add error handling on: sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE); See also PR bpo-22049. |
The SIGINT event is also needed by PyOS_Readline, _io (builtin), _winapi (builtin), and _multiprocessing (pyd). Is the time module guaranteed to be imported in 3.x? It appears to get imported incidentally via _PyImportZip_Init (zipimport). Could sigint_event be relocated to a platform-dependent extension of _PyRuntimeState that gets initialized and finalized independent of any module? |
This, or leaving it where it is and making sure it gets initialised (it really is about emulating certain signals). Either sounds fine to me. |
Ok, the issue should now be fixed in 3.8, 3.9 and master branches. Thanks for the bug report hafiz bilal. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: