<div class="gmail_quote">On Fri, Jun 18, 2010 at 12:55 PM, Szabolcs Balogh <span dir="ltr">&lt;<a href="mailto:bszabolcs@gmail.com">bszabolcs@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5"><br>
</div></div>It is possible to:<br>
 - start the reactor when I initialize my program (eg. in main)<br>
 - call connectTCP or connectSSL when I want and every time I want?<br>
<br></blockquote><div><br>Yes, this is exactly what you are supposed to do.  Call the reactor _once_ and once only.<br><br>The call connectTCP or whatever whenever is appropriate for your program.<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

Something like this:<br>
<br>
def check_messages(account):<br>
        # connect to imap server and check for new mailboxes/messages<br>
        reactor.connectSSL(...)<br>
<br>
<br>
if __name__ == &quot;__main__&quot;:<br>
        reactor.run()<br>
<br>
        check_message(account1)<br>
        check_message(account2)<br>
        ...<br>
        do something else<br>
        ...<br>
        check_message(accountn)<br>
<br>
<br>
Because I can&#39;t implement this :}<br>
<br>
I have started the implementation based on &quot;Twisted Network Programming<br>
Essential&quot;, but this book doesn&#39;t treat multiple connections...<br>
<div><div></div><div class="h5"><br></div></div></blockquote></div><br>The above won&#39;t work, because run() blocks.  So in your example here, check_messages() won&#39;t be called until after the run() call returns, which means the reactor won&#39;t be running, which means check_messages won&#39;t work.<br>
<br>look into callWhenRunning: <a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.internet.interfaces.IReactorCore.html#callWhenRunning">http://twistedmatrix.com/documents/10.0.0/api/twisted.internet.interfaces.IReactorCore.html#callWhenRunning</a><br>
<br>and do something like:<br><br>def start_checking():<br>    (do your check_messages stuff here)<br><br>if __name__ == &#39;__main__&#39;:<br>    reactor.callWhenRunning(start_checking)<br>    <br>    reactor.run()<br><br>
using callWhenRunning isn&#39;t necessary, but I find that it clarifies things for me sometimes<br><br>reactor.run() is usually the _last_ thing I call in my Twisted programs, unless I have some shutdown cleanup to do, and even that is usually better done in the reactor shutdown hook  <br>
(see: <a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.internet.interfaces.IReactorCore.html#addSystemEventTrigger">http://twistedmatrix.com/documents/10.0.0/api/twisted.internet.interfaces.IReactorCore.html#addSystemEventTrigger</a>)<br>
<br>you also need to figure out how to structure your program flow using deferreds, which is not always easy, and depends greatly on your specific application.<br><br>remember that deferreds are just a way of organizing callback functions<br>
<br>you can &quot;chain&quot; deferreds something like this (assuming your functions return Deferreds):<br><br>d = check_first_one()<br>d.addCallback(check_second_one, ...)<br><br>etc...<br><br>which will make check_second_one start after check_first_one completes<br>
<br>You can also use a deferredList, which is often handy when you want to keep track of several deferred operations which are operating simultaneously<br>(I haven&#39;t used one in a while, so I don&#39;t have a simple example off the top of my head, but there&#39;s a simple example in the Twisted book)<br>
<br>Hope this helps,<br><br>Kevin Horn<br><br><div style="visibility: hidden; display: inline;" id="avg_ls_inline_popup"></div><style type="text/css">#avg_ls_inline_popup {  position:absolute;  z-index:9999;  padding: 0px 0px;  margin-left: 0px;  margin-top: 0px;  width: 240px;  overflow: hidden;  word-wrap: break-word;  color: black;  font-size: 10px;  text-align: left;  line-height: 13px;}</style>