[Twisted-Python] win 32 reactor
![](https://secure.gravatar.com/avatar/41103198ecac8dc95025c4417de593ad.jpg?s=120&d=mm&r=g)
Hello, I need to use PyHook, a win32 lib allowing global input monitoring (mouse, keyboard, etc). It uses the PyWin32 extentions, in particular win32com's PumpMessages() function. This is an event loop defined like so : // @pymethod |pythoncom|PumpMessages|Pumps all messages for the current thread until a WM_QUIT message. static PyObject *pythoncom_PumpMessages(PyObject *self, PyObject *args) { MSG msg; int rc; Py_BEGIN_ALLOW_THREADS while ((rc=GetMessage(&msg, 0, 0, 0))==1) { TranslateMessage(&msg); // needed? DispatchMessage(&msg); } Py_END_ALLOW_THREADS if (rc==-1) return PyWin_SetAPIError("GetMessage"); Py_INCREF(Py_None); return Py_None; } my question is ho can I make this work with twisted? Is there a way to use the supplied win32 reactors (there are three I think) or do I have to write one myself? Could someone please point me in the right direction? Thanks, Gabriel
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Mon, 31 Mar 2008 18:28:09 +0200, Gabriel Rossetti <mailing_lists@evotex.ch> wrote:
The three reactors you're probably referring to are selectreactor, win32er, and IOCP reactor. Of these, only the latter two do anything particular to Windows (the first just uses select(2), which Windows sort of has). win32er is basically a loop around MsgWaitForMultipleObjects. IOCP reactor is basically a loop around GetQueuedCompletionStatus. One way to look at your question is to consider how GetMessage/TranslateMessage/DispatchMessage interact with MWFMO and GQCS. This is basically a Windows API question, so I can't answer it, since I know very little about Windows APIs. Jean-Paul
![](https://secure.gravatar.com/avatar/41bb9450b145ffb3ceb8148137792ab7.jpg?s=120&d=mm&r=g)
Jean-Paul Calderone wrote:
The problem on Windows is that there can only be one event loop running at one time, but every library wants supply its own. This seem to be true for the code Gabriel posted, and is certainly true of the reactors JP mentioned, plus anything as ordinary as COM, the win32 MessageBox function, or resizing a window! This leads to a problem where your application will receive no twisted events while your window is being resized, or a message box is visible. IMO the best way to integrate twisted on win32 involves one of the threaded reactors. This runs the select loop (or WFMO loop) in a separate thread, then uses PostMessage to wake up your main thread for it to handle the network events inside a message handler. For your application code this all still looks like a single threaded model. I hope this helps,
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Mon, 31 Mar 2008 18:28:09 +0200, Gabriel Rossetti <mailing_lists@evotex.ch> wrote:
The three reactors you're probably referring to are selectreactor, win32er, and IOCP reactor. Of these, only the latter two do anything particular to Windows (the first just uses select(2), which Windows sort of has). win32er is basically a loop around MsgWaitForMultipleObjects. IOCP reactor is basically a loop around GetQueuedCompletionStatus. One way to look at your question is to consider how GetMessage/TranslateMessage/DispatchMessage interact with MWFMO and GQCS. This is basically a Windows API question, so I can't answer it, since I know very little about Windows APIs. Jean-Paul
![](https://secure.gravatar.com/avatar/41bb9450b145ffb3ceb8148137792ab7.jpg?s=120&d=mm&r=g)
Jean-Paul Calderone wrote:
The problem on Windows is that there can only be one event loop running at one time, but every library wants supply its own. This seem to be true for the code Gabriel posted, and is certainly true of the reactors JP mentioned, plus anything as ordinary as COM, the win32 MessageBox function, or resizing a window! This leads to a problem where your application will receive no twisted events while your window is being resized, or a message box is visible. IMO the best way to integrate twisted on win32 involves one of the threaded reactors. This runs the select loop (or WFMO loop) in a separate thread, then uses PostMessage to wake up your main thread for it to handle the network events inside a message handler. For your application code this all still looks like a single threaded model. I hope this helps,
participants (4)
-
Andreas Kostyrka
-
Gabriel Rossetti
-
Jean-Paul Calderone
-
Toby Dickenson