Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>How can I make callbacks outside the class?</p> </blockquote> <p>This sounds like a very common misunderstanding. As a result of the misunderstanding, the question, as asked, doesn't make a lot of sense. So let's forget about the question.</p> <p>You already have some code that's using Deferreds. Let's start with <code>mainmyServ</code>:</p> <pre><code>def mainmyServ(self): # print "Client ID is: " + self.myServID # Joining Chat s1= DeferredList([client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=join&amp;user=%s&amp;message=null" % self.myServID),]) s1.addCallback(self.connectedtomyServService) s1.addErrback(self.error) </code></pre> <p>First, you can get rid of the <code>DeferredList</code>. Your list only has one <code>Deferred</code> in it, so the <code>DeferredList</code> isn't adding any value. You'll get practically the same behavior like this, and your callbacks can be simplified by removing all of the <code>[0][0]</code> expressions.</p> <pre><code>def mainmyServ(self): # print "Client ID is: " + self.myServID # Joining Chat s1= client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=join&amp;user=%s&amp;message=null" % self.myServID) s1.addCallback(self.connectedtomyServService) s1.addErrback(self.error) </code></pre> <p>So this is a perfectly reasonable method which is calling a function that returns a Deferred and then adding a callback and an errback to that Deferred. Say you have another function, perhaps your overall main function:</p> <pre><code>def main(): service = ChatService() service.mainmyServ() </code></pre> <p>What prevents the <code>main</code> function from adding more callbacks to the <code>Deferred</code> in <code>mainmyServ</code>? Only that <code>mainmyServ</code> doesn't bother to return it. So:</p> <pre><code>def mainmyServ(self): # print "Client ID is: " + self.myServID # Joining Chat s1= client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=join&amp;user=%s&amp;message=null" % self.myServID) s1.addCallback(self.connectedtomyServService) s1.addErrback(self.error) return s1 def main(): service = ChatService() d = service.mainmyServ() d.addCallback(doSomethingElse) </code></pre> <p>Nothing special there, it's just another <code>addCallback</code>. All you were missing was a reference to the <code>Deferred</code>.</p> <p>You can set up your loop now, by having <code>doSomethingElse</code> call another method on <code>ChatService</code>. If that other method returns a <code>Deferred</code>, then <code>doSomethingElse</code> can add a callback to it that calls <code>mainmyServ</code> again. And so on. There's your loop, controlled "outside" of the class.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload