53 | | You'll notice that the client.connect() function returns a Twisted Deferred object. If you are not familiar with Twisted or Deferred objects, then I strongly suggest [http://twistedmatrix.com/projects/core/documentation/howto/defer.html reading about them] before continuing as we will be using Deferreds throughout these examples. |
| 55 | So now that we've got a basic script to get us connected to a daemon, let's extend it a bit to do something useful. We'll start by adding a remote procedure call in the '''on_connect_success()''' function. |
| 56 | |
| 57 | {{{ |
| 58 | #!python |
| 59 | |
| 60 | def on_connect_success(result): |
| 61 | print "Connection was successful!" |
| 62 | def on_get_config_value(key, value): |
| 63 | print "Got config value from the daemon!" |
| 64 | print "%s: %s" % (key, value) |
| 65 | # Disconnect from the daemon once we've got what we needed |
| 66 | client.disconnect() |
| 67 | # Stop the twisted main loop and exit |
| 68 | reactor.stop() |
| 69 | |
| 70 | # Request the config value for the key 'download_location' |
| 71 | client.core.get_config_value("download_location").addCallback(on_get_config_value, "download_location") |
| 72 | |
| 73 | }}} |
| 74 | |
| 75 | Ok! We now should be getting a print out of the ''download_location'' config value. You'll notice that any RPC method returns a Deferred object, just like the '''client.connect()''' method. Since the '''core.get_config_value()''' method only returns the value we are passing the ''key'' to the callback function too. So in the '''on_get_config_value()''' callback, the first argument is from our '''addCallback()''' call and the second is the return value from the daemon. |
| 76 | |