[Twisted-Python] Twisted communication with embedded C thread

William Waites ww at groovy.net
Thu Jul 7 14:19:50 EDT 2005

On Thu, Jul 07, 2005 at 01:48:57PM -0400, Tom Cocagne wrote:
>     All I really need is a means by which to inform Twisted's reactor
> that a C thread has sent a message.  My original plan was to use a
> pair of pipes (via os.pipe()) to send pointers back and forth but it
> didn't seem to work on Windows . I'm pretty sure this will work fine
> for posix systems but it definitely didn't work on my first attempt
> with Windows (though the problem may have been due to my library
> configuration).

#define MY_MODULE "foo.bar"
#define MY_FUNCTION "handle_message"

void send_message_to_python(message_t *message)
      PyObject *module_name, *module, *dict, *func, *args, *pymessage;

      module_name = PyString_FromString(MY_MODULE);
      module = PyImport_Import(module_name);
      dict = PyModule_GetDict(module)
      func = PyDict_GetItemString(MY_FUNCTION);

      args = PyTuple_New(1)
      pymessage = MyPythonWrappedMessage_New(message);
      PyTuple_SetItem(args, 0, pymessage);

      PyObject_CallObject(func, args);

(with appropriate error checking, of course)

This has the effect of doing

      from foo.bar import handle_message

I'm not at all sure what would happen on Windows. I suppose this would
work there too.

But you also need to run the reactor, I guess which might be easier or
harder depending on the behaviour of the C program. The simplest way
is just to start a C thread and do the equivalent of the above for

	from twisted.internet import reactor


More information about the Twisted-Python mailing list