You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
_multiprocessing.Connection.poll with timeout uses polling under Windows #55877
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:
assignee='https://github.com/briancurtin'closed_at=<Date2012-04-01.14:03:25.955>created_at=<Date2011-03-25.00:27:34.380>labels= ['performance']
title='_multiprocessing.Connection.poll with timeout uses polling under Windows'updated_at=<Date2012-04-01.14:03:25.954>user='https://github.com/pitrou'
This impacts higher-level APIs such as Queue.get() with a positive timeout.
I'm not sure whether it's easily possible to fix this. A Google search seems to suggest that the pipe could be opened with "SYNCHRONIZE" access rights (?) and then WaitForSingleObject() be used on the handle.
SYNCHRONIZE comes for free on pipes created with CreateNamedPipe, so there's nothing to do there.
I think it's more likely that we'll have to use WaitForMultipleObjects and include the pipe handle with a signal handler for Ctrl-C. I believe this is done elsewhere in the code, timemodule comes to mind (for sleep).
Attaching an initial patch implementing the same functionality but using WaitForMultipleObjects. Here's some details:
WFMO takes an array of objects to wait on, which is the pipe handle and sigint_event which is a handle to an event which gets set when CTRL-C is caught (see Modules/_multiprocessing/multiprocessing.c). Waiting for both objects replaces the need to write a custom loop which periodically calls PyErr_CheckSignals.
A negative timeout was effectively a blocking call, so we can let WFMO handle that with an INFINITE timeout setting.
WFMO returns the object which raised it relative from event 0, so the switch case for a CTRL-C is 0+1 and returns the same value as before. If the pipe was the object to raise, just like before: return true if there's data, false if not.
kristjanvalur commented on Mar 26, 2011
kristjanvalurmannequin
Mannequin
More actions
I don't think it is necessary to have the initial call to PeekNamedPipe. It is a kernel call just like WFMO and so just as expensive. It also has some strange semantics, (such as being possibly blocking in a multithreaded application. read the docs...). Just go for the WFMO to keep things simple.
/* handle result */
if (res != WAIT_OBJECT_0 + 1)
break;
/* got SIGINT so give signal handler a chance to run */
Sleep(1);
/* if this is main thread let KeyboardInterrupt be raised */
if (PyErr_CheckSignals())
return NULL;
This doesn't seem to be so easy. WFMO (or WFSO) often seems to return successfully while there's no message to read on the pipe end. Here is a sample session (disturbing results):
(do note how the end created with CreateFile() is considered ready by WaitForMultipleObjects, while the end created with CreateNamedPipe() is considered ready after an unsuccessful call to PeekNamedPipe()!)
A solution could be to use overlapped I/O on the named pipe handles.
The first poll() would call ReadFile() with a tiny value (1?) and store an object wrapping the OVERLAPPED structure. Subsequent poll() or recv() would re-use that structure until the overlapped read succeeds.
The hEvent in the OVERLAPPED structure should be usable in WFMO fine.
Activity
pitrou commentedon Mar 25, 2011
This impacts higher-level APIs such as Queue.get() with a positive timeout.
I'm not sure whether it's easily possible to fix this. A Google search seems to suggest that the pipe could be opened with "SYNCHRONIZE" access rights (?) and then WaitForSingleObject() be used on the handle.
pitrou commentedon Mar 25, 2011
For the record, the polling code is in conn_poll() in Modules/_multiprocessing/pipe_connection.c.
Windows named pipes seem to be created in pure Python in Lib/multiprocessing/connection.py.
briancurtin commentedon Mar 25, 2011
SYNCHRONIZE comes for free on pipes created with CreateNamedPipe, so there's nothing to do there.
I think it's more likely that we'll have to use WaitForMultipleObjects and include the pipe handle with a signal handler for Ctrl-C. I believe this is done elsewhere in the code, timemodule comes to mind (for sleep).
I'll see what I can come up with.
briancurtin commentedon Mar 26, 2011
Attaching an initial patch implementing the same functionality but using WaitForMultipleObjects. Here's some details:
WFMO takes an array of objects to wait on, which is the pipe handle and sigint_event which is a handle to an event which gets set when CTRL-C is caught (see Modules/_multiprocessing/multiprocessing.c). Waiting for both objects replaces the need to write a custom loop which periodically calls PyErr_CheckSignals.
A negative timeout was effectively a blocking call, so we can let WFMO handle that with an INFINITE timeout setting.
WFMO returns the object which raised it relative from event 0, so the switch case for a CTRL-C is 0+1 and returns the same value as before. If the pipe was the object to raise, just like before: return true if there's data, false if not.
kristjanvalur commentedon Mar 26, 2011
I don't think it is necessary to have the initial call to PeekNamedPipe. It is a kernel call just like WFMO and so just as expensive. It also has some strange semantics, (such as being possibly blocking in a multithreaded application. read the docs...). Just go for the WFMO to keep things simple.
pitrou commentedon Mar 26, 2011
Are you sure about MP_EXCEPTION_HAS_BEEN_SET?
semaphore.c has a more sophisticated logic:
pitrou commentedon May 7, 2011
This doesn't seem to be so easy. WFMO (or WFSO) often seems to return successfully while there's no message to read on the pipe end. Here is a sample session (disturbing results):
(do note how the end created with CreateFile() is considered ready by WaitForMultipleObjects, while the end created with CreateNamedPipe() is considered ready after an unsuccessful call to PeekNamedPipe()!)
pitrou commentedon May 7, 2011
A solution could be to use overlapped I/O on the named pipe handles.
The first poll() would call ReadFile() with a tiny value (1?) and store an object wrapping the OVERLAPPED structure. Subsequent poll() or recv() would re-use that structure until the overlapped read succeeds.
The hEvent in the OVERLAPPED structure should be usable in WFMO fine.
pitrou commentedon May 7, 2011
I have a full patch using overlapped I/O in bpo-9205 (sentinels3.patch).
pitrou commentedon Apr 1, 2012
This is totally outdated by the new Connection implementation in 3.3.