[Twisted-Python] another probably very simple question

[whoops, sorry about the previous incomplete message] this is actually the first time i've ever written a PB client in python. since my java client was calling the remote methods out of the awt/swing event thread, i never had to deal with this particular issue. while i'm waiting for the update to the java pb packages (*cough*itamar*cough* ;-) ), i figured it would be worthwhile to do up a text-based client in python. i know this relates to my poor understanding of deferreds, but i just can't figure out exactly how to go about doing what i want to do. i want to be able to continually prompt the user for a string that gets sent to a "parse" method on the remote object. i'm not exactly sure the right way to do this. do I just add a callback to the deferred returned by call remote? here's my code so far: def main(): config = Options() if(config['login'] == None): config['login'] = getpass.getuser() passwd = getpass.getpass("%s@%s's password: " % (config['login'], config['host'])) client = TextClient() factory = pb.PBClientFactory() reactor.connectTCP(config['host'], int(config['port']), factory) defer = factory.login(credentials.UsernamePassword(config['login'], passwd), client) defer.addCallback(connected) reactor.run() def connected(perspective): command = raw_input("> "); defer = perspective.callRemote("parse", command) defer.addCallback(connected)

On Mon, Nov 17, 2003 at 05:34:24PM -0500, Phil Christensen wrote:
Calling raw_input is a bad idea. It will block. You should either use twisted.internet.stdio (which is probably more work than you care to do -- but see doc/examples/cursesclient.py for an example that uses stdio and curses to make a simple irc client), or run raw_input in a thread: from twisted.internet import threads def connected(perspective): deferred = threads.deferToThread(raw_input, "> ") deferred.addCallback(lambda cmd: perspective.callRemote("parse", cmd)) deferred.addCallback(lambda res: connected(perspective)) Note that perspective.callRemote will return a Deferred -- but that's ok, the Deferreds automatically cope with (i.e. wait for) deferred results from callbacks :) Something else to notice is that Deferreds pass the result of the previous callback (or the original result, for the first callback) to each callback, which is why I wrote: deferred.addCallback(lambda res: connected(perspective)) rather than your: defer.addCallback(connected) -Andrew.

So i followed Andrew's 'raw_input' advice with much success, but i naturally wanted to dive into curses. I was checking out the cursesclient.py file in doc/examples/, but it seems to be broken... it connects and everything just fine, but as soon as i type anything, i get the following: ------------------------------------------------------------- --- <exception caught here> --- File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/ site-packages/twisted/python/log.py", line 60, in callWithLogger lp = logger.logPrefix() exceptions.AttributeError: Screen instance has no attribute 'logPrefix' ------------------------------------------------------------- any ideas? -phil

On Wed, Nov 19, 2003 at 06:15:15PM -0500, Phil Christensen wrote:
It's already been fixed in CVS: http://cvs.twistedmatrix.com/cvs/doc/examples/cursesclient.py?rev=1.7&content-type=text/vnd.viewcvs-markup -Andrew.

On Thu, Nov 20, 2003 at 10:49:27AM +1100, Andrew Bennetts wrote:
It's already been fixed in CVS:
Ok, I've heard this enough times. I'm going to release 1.1.1. -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com/

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Is there a counter? Do you tie knots in string? :-) C Christopher Armstrong wrote: | On Thu, Nov 20, 2003 at 10:49:27AM +1100, Andrew Bennetts wrote: | |>It's already been fixed in CVS: | | | Ok, I've heard this enough times. I'm going to release 1.1.1. | -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.3-nr1 (Windows 2000) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQE/vArt3A5SrXAiHQcRAk4vAKCsa8U/jmDGGmsJZrK0ljZkloq54QCfbsbd 94xVAAqhXge/t/14l7lz+mA= =uIxg -----END PGP SIGNATURE-----

On Mon, Nov 17, 2003 at 05:34:24PM -0500, Phil Christensen wrote:
Calling raw_input is a bad idea. It will block. You should either use twisted.internet.stdio (which is probably more work than you care to do -- but see doc/examples/cursesclient.py for an example that uses stdio and curses to make a simple irc client), or run raw_input in a thread: from twisted.internet import threads def connected(perspective): deferred = threads.deferToThread(raw_input, "> ") deferred.addCallback(lambda cmd: perspective.callRemote("parse", cmd)) deferred.addCallback(lambda res: connected(perspective)) Note that perspective.callRemote will return a Deferred -- but that's ok, the Deferreds automatically cope with (i.e. wait for) deferred results from callbacks :) Something else to notice is that Deferreds pass the result of the previous callback (or the original result, for the first callback) to each callback, which is why I wrote: deferred.addCallback(lambda res: connected(perspective)) rather than your: defer.addCallback(connected) -Andrew.

So i followed Andrew's 'raw_input' advice with much success, but i naturally wanted to dive into curses. I was checking out the cursesclient.py file in doc/examples/, but it seems to be broken... it connects and everything just fine, but as soon as i type anything, i get the following: ------------------------------------------------------------- --- <exception caught here> --- File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/ site-packages/twisted/python/log.py", line 60, in callWithLogger lp = logger.logPrefix() exceptions.AttributeError: Screen instance has no attribute 'logPrefix' ------------------------------------------------------------- any ideas? -phil

On Wed, Nov 19, 2003 at 06:15:15PM -0500, Phil Christensen wrote:
It's already been fixed in CVS: http://cvs.twistedmatrix.com/cvs/doc/examples/cursesclient.py?rev=1.7&content-type=text/vnd.viewcvs-markup -Andrew.

On Thu, Nov 20, 2003 at 10:49:27AM +1100, Andrew Bennetts wrote:
It's already been fixed in CVS:
Ok, I've heard this enough times. I'm going to release 1.1.1. -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com/

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Is there a counter? Do you tie knots in string? :-) C Christopher Armstrong wrote: | On Thu, Nov 20, 2003 at 10:49:27AM +1100, Andrew Bennetts wrote: | |>It's already been fixed in CVS: | | | Ok, I've heard this enough times. I'm going to release 1.1.1. | -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.3-nr1 (Windows 2000) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQE/vArt3A5SrXAiHQcRAk4vAKCsa8U/jmDGGmsJZrK0ljZkloq54QCfbsbd 94xVAAqhXge/t/14l7lz+mA= =uIxg -----END PGP SIGNATURE-----
participants (5)
-
Andrew Bennetts
-
Christopher Armstrong
-
Cory Dodt
-
Glyph Lefkowitz
-
Phil Christensen