Deferred framework is probably more like BOOST::ASIO.  With callbacks,
your adding code to be executed in a "reactor" thread ( ie. the thread
from which reactor.run() is called ).  This way you can shoot of a
separate thread ( say with deferToThread ) to do some work, queue up
some callbacks then "fire" the callbacks in the reactor thread.

node_clicked is probably expected to return a deferred.  If you have the
return value already, you can return defer.succeed or defer.fail.  Read
the twisted docs about Generating Deferreds etc.

On Tue, 2009-02-03 at 22:37 +0100, Kristian Lippert wrote:
>  Hi 
> I have a play-method in my own stream-player and it works:
> 
>     def node_clicked(self, widget, item):
>         def play_uri(self, station):
>             # We already know the real URL of the playable audio
> stream, no
>             # need to query the resource manager
>             # get player and start playing
>             player =
> self.frontend.retrieve_controllers('/poblesec/music_player')[0]
>             player.player.play_model(station)
>             main = self.frontend.retrieve_controllers('/poblesec')[0]
>             main.show_music_player()
>             self.stop_loading_animation()
> 
>         pdb.set_trace()
>         if isinstance(item, Action):
>             item.run()
>         elif isinstance(item, PlayableModel):
>             play_uri(self, item)
> 
> It works fine but if there are problems with the stream it will hang
> for a while, so I thought I could use a deferred, like:
> 
>     def node_clicked(self, widget, item):
> 
>         def play_uri(self, station):
>             # We already know the real URL of the playable audio
> stream, no
>             # need to query the resource manager
>             # get player and start playing
>             player =
> self.frontend.retrieve_controllers('/poblesec/music_player')[0]
>             player.player.play_model(station)
> 
>             main = self.frontend.retrieve_controllers('/poblesec')[0]
>             main.show_music_player()
>             self.stop_loading_animation()
> 
>         pdb.set_trace()
>         if isinstance(item, Action):
>             item.run()
>         elif isinstance(item, PlayableModel):
>             dfr = defer.Deferred().addCallback(play_uri)
>             return dfr(self, item)
> 
> My problem is that I am not sure:
> 1) Do I create the deferred in the right manner?
> 2) How does the binding of the parameters happen?
> 3) Do I need to activate the deferred my self or will this happen by
> magic by it self?
> 
> As far as I can read the deferred can be compared to the .NET
> asynchronous delegates or boost::thread. Boost::thread is much simpler
> but they share the problem regarding binding variables.
> 
> Please help me fix the code!
> 
> Best Regards,
> Kristian
> 
> ______________________________________________________________________
> Get news, entertainment and everything you care about at Live.com.
> Check it out!

Reply via email to