[Twisted-Python] Conch: multiple commands

Brendan Simon Brendan at BrendanSimon.com
Thu Feb 9 16:02:40 EST 2006


Paul Swartz <paulswartz at gmail.com> wrote:
>> Is it possible to open _one_ channel, and send commands in a 
>> synchronously?  ie. wait for commands to return output and then issue 
>> other commands if necesary??
>> Maybe this goes against the grain of the Twisted Asynchronous Framework ???
>>     
> You can send one command at a time, but you still have to write the code 
> asynchronously.
>   
I think this is half my problem, working out how to use the asynchronous 
framework properly ;-)

>>    def servicesStarted(self):
>>        self.openChannel(CommandChannel.openChannel('ls /tmp/dir1')
>>        # how do I wait for list of dir1 to finish?
>>        self.openChannel(CommandChannel.openChannel('ls /tmp/dir2')
>>        # how do I wait for list of dir2 to finish?
>>        self.openChannel(CommandChannel.openChannel('ls /tmp/dir3')
>>        # how do I wait for list of dir3 to finish?
>>     
> What I'd do is also pass a Deferred to CommandChannel, that is 
> call/errbacked when the command finishes.
>
> def servicesStarted(self):
>      d = defer.Deferred()
>      d.addCallback(self._cbFirstLs)
>      d.addErrback(self._ebFirstLs)
>      self.openChannel(CommandChannel(d, "ls /tmp/dir1"))
>
> def _cbFirstLs(self, result):
>      print 'directory listing', result
>      d = defer.Deferred()
>      self.openChannel(CommandChannel(d, "ls /tmp/dir2"))
>
> def _ebFirstLs(self, f):
>      log.err()
>
> etc., for all the commands you want to run.
>   
Thanks.  I'll play with this a little more.  Guess I may need some kind 
of state machine in the callback function if different unrelated 
commands are to be sent at different times based on various application 
events.  Does that sound like a good way to use Twisted???

Thanks,
Brendan.





More information about the Twisted-Python mailing list