&nbsp;&nbsp;&nbsp; The C++ library is completely independent of Python and Twisted but it can be made to work with them. One of the initial uses of it, in fact, was to run a Python interpreter (using Twisted) in a dedicated C++ thread and use it to direct the actions of all the other threads in the process. <br>
&nbsp;<br>&nbsp;&nbsp;&nbsp; The way I did it is this:<br><br>&nbsp;&nbsp;&nbsp; 1. Spawn a C++ thread to run the Python interpreter inside of. All use of the Python C-API must be done from within this thread if you want to avoid dealing with the Global Interpreter Lock (remember Python itself is a single-threaded application). Other C++ threads needing to call a Python C-API function can do so using a DeferredCommand object that wraps the call they want to make. Hand this object to the Python thread for execution and use the Deferred mechanism to get the answer back to the original thread..... Basically, you turn the interpreter into one big Active Object as per the standard design pattern.<br>
<br>&nbsp;&nbsp;&nbsp; 2. Use the Python C-API to import a Python module you wrote that will initialize Twisted and create a pair of TCP sockets that are connected to each other. Then use the C-API to get the OS-level file descriptor to one of the sockets. The purpose of this is to provide a mechanism for other C++ threads to inform the Twisted reactor that there&#39;s something in the C++ world it needs to do. When one of those DeferredCommands in step one is given to the Python thread, the last thing the delivering thread needs to do is call a function that writes a single byte to the TCP socket. This will cause the Twisted reactor in the Python thread to wake up from it&#39;s &#39;select&#39; call and dispatch a handler. This handler can then execute a C++ function you registered with the C-API. Once back in the C++ world and in the context of the Python thread,&nbsp; you can dispatch any of the DeferredCommands sitting in the thread&#39;s queue.<br>
<br>&nbsp;&nbsp;&nbsp; I don&#39;t currently have a mechanism for tying a Twisted Deferred directly to one of the C++ deferreds since I haven&#39;t yet found a use-case where it makes sense to do so. It&#39;s probably possible though.<br>
<br>&nbsp;&nbsp;&nbsp; As for using the C++ deferreds with boost&#39;s asio... I haven&#39;t used asio yet but a quick read through the API makes it looks like an almost perfect match for these deferreds. In fact, I&#39;d like to eventually put this code up for consideration by the boost community for eventually including something like it in boost. I *highly* doubt they&#39;d want to use the code I&#39;ve written but it might serve as a good proof-of-concept at least.<br>
<br>&nbsp;&nbsp;&nbsp; Tom<br><br><div class="gmail_quote">On Fri, Jan 30, 2009 at 12:09 AM, V S P <span dir="ltr">&lt;<a href="mailto:toreason@fastmail.fm">toreason@fastmail.fm</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi, this is very interesting<br>
( am I currently testing out C++ web server (using boost asio) called<br>
pion<br>
to see if can suit my needs (it is a multi-threaded async IO generic<br>
server and boost asio uses the best async APIs for each OS (including<br>
kques for freebsd for example).<br>
<br>
<br>
I looked at the example threads.cpp -- but could not figure out -- how<br>
would it integrate with Twisted ?<br>
<br>
thanks<br>
<div><div></div><div class="Wj3C7c"><br>
On Thu, 29 Jan 2009 12:21:36 -0800, &quot;Tom Cocagne&quot;<br>
&lt;<a href="mailto:tom.cocagne@gmail.com">tom.cocagne@gmail.com</a>&gt; said:<br>
&gt; &nbsp; &nbsp; The question of whether anyone had developed a C++ implementation of<br>
&gt; Twisted&#39;s Deferreds has cropped up on this list several times over the<br>
&gt; last<br>
&gt; few years. To date, I&#39;ve unfortunately been forced to sit on an<br>
&gt; implementation I developed several years ago for the Department of<br>
&gt; Defense.<br>
&gt; Due to some rather draconian IP &amp; security policies, it&#39;s taken an<br>
&gt; exceptionally long time to get the code released. I&#39;m happy to announce<br>
&gt; though that I finally managed to obtain release authorization for it and<br>
&gt; that the code now has a new home on Sourceforge.<br>
&gt;<br>
&gt; &nbsp; &nbsp; As C++ has differing language features, the API of the C++ deferreds<br>
&gt; isn&#39;t identical to those of Twisted&#39;s but most of the key concepts mapped<br>
&gt; over pretty well. Additionally, this implementation has a few design<br>
&gt; extensions to facilitate the development multi-threaded C++ applications.<br>
&gt; The multi-threaded aspect grew almost accidentally out of the original<br>
&gt; single-threaded design but, since then, it has proven to be<br>
&gt; extraordinarily<br>
&gt; useful for simplifying several multi-threaded applications I&#39;ve worked<br>
&gt; on.<br>
&gt;<br>
&gt; &nbsp; &nbsp; The implementation of the library is, however, rather complex. In<br>
&gt; &nbsp; &nbsp; large<br>
&gt; part, this is due to the effort required to work around the inherent<br>
&gt; inflexibility of the C++ type system. The Function, Bind, and<br>
&gt; Meta-Programming libraries found in Boost have gone a long way towards<br>
&gt; simplify the implementation though (my original Loki-based attempt was<br>
&gt; almost incomprehensible even to me ;-). The library is fully documented<br>
&gt; though so as long as you&#39;re reasonably comfortable with C++ templates, it<br>
&gt; should be understandable.<br>
&gt;<br>
&gt; &nbsp; &nbsp; So far, the library has only been exposed to a handful of individuals<br>
&gt; for review. I&#39;d be interested to hear the comments anyone has about the<br>
&gt; design and/or implementation approach. The code is available at:<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://deferred.svn.sourceforge.net/viewvc/deferred/" target="_blank">http://deferred.svn.sourceforge.net/viewvc/deferred/</a><br>
&gt;<br>
&gt; &nbsp; &nbsp; Cheers,<br>
&gt;<br>
&gt; &nbsp; &nbsp; Tom<br>
</div></div>--<br>
 &nbsp;V S P<br>
 &nbsp;<a href="mailto:toreason@fastmail.fm">toreason@fastmail.fm</a><br>
<br>
--<br>
<a href="http://www.fastmail.fm" target="_blank">http://www.fastmail.fm</a> - Accessible with your email software<br>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;or over the web<br>
<br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</blockquote></div><br>