[Twisted-Python] Multiple clients

Gerrat Rickert grickert at coldstorage.com
Fri Jun 18 13:23:50 MDT 2010


>It is possible to:
> - start the reactor when I initialize my program (eg. in main)
> - call connectTCP or connectSSL when I want and every time I want?
>
>Something like this:
>
>def check_messages(account):
>	# connect to imap server and check for new mailboxes/messages
>	reactor.connectSSL(...)
>
>
>if __name__ == "__main__":
>	reactor.run()
>
>	check_message(account1)
>	check_message(account2)
>	...
>	do something else
>	...
>	check_message(accountn)
>
>
>Because I can't implement this :}


Something like this (obviously won't work verbatim 
& may not be the best example, 
but hopefully will give you the idea):

def check_messages(account):
    # connect to imap server and check for new mailboxes/messages
    reactor.connectSSL(...)
    # should return a deferred!

def do_something_else():
    return defer.succeed(None)  # return a deferred here too

def something_failed(rslt):
  print rslt
  reactor.stop()

def call_my_stuff(accountList):

  # check messages asynchronously
  checks = [check_message(account) for account in accountList]

  dl = defer.DeferredList(checks)
  dl.addCallback(do_something_else) # wait until all messages checked to
run
  dl.addCallback(runner, accountList) # wait till we've done something
else
  dl.addErrback(something_failed) # ALWAYS attach errbacks

def runner(accountList):
  reactor.callLater(0.1, call_my_stuff, accountList)

if __name__ == "__main__":

    accountList = [account1, account2]
    reactor.callWhenRunning(runner, accountList)
    reactor.run()




More information about the Twisted-Python mailing list