Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6945891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:29:41+00:00 2026-05-27T13:29:41+00:00

I’m trying to write a longpolling server with Twisted, but I’m afraid I’m not

  • 0

I’m trying to write a longpolling server with Twisted, but I’m afraid I’m not understanding the request management.

When the client navigates away from the page, I can see it continue to loop in the console. I thought the instance would just destroy itself when the client disconnects.

And that is my goal, I want to run the logic in DataService individually for each client that connects.

Here’s what I have: (i chopped this code down for easy consumption, might be some syntax errors because of that, but it came from working code)

class DataService(Resource):
    def __init__(self, sid):
        # set looping call
        self.loopingCall = task.LoopingCall(self.__print_data)
        self.loopingCall.start(5, False)
        Resource.__init__(self)
        self.sid = int(sid);
        self.finished = False

        # initialize response
        self.response = response = {'status':1, 'message': 'OK', 'time':int(time.time()), 'data': {}}

    def connectionLost(self, reason):
        self.loopingCall.stop();

    def render_GET(self, request):
        # response will be json format
        request.setHeader('Content-Type', 'application/json')
        # make sure required GET vars exist
        if 'lastupdate' not in request.args:
            self.loopingCall.stop()
            return ujson.dumps({'status':0,'message':'invalid query','data':{}})

        # set last update timestamp from query string
        self.lastupdate = int(request.args['lastupdate'][0])

        # set self.request so we can access it in __print_data
        self.request = request
        # call print data
        self.__print_data()

        if not self.finished:
            return server.NOT_DONE_YET

    def __print_data(self):

        # set updated data
        if self.lastupdate < self.myappobj.lastupdate
            self.response{'data']['items'] = {'foo':'bar'}

        # if updates were found, close loop, print, and finish request
        if len(response['data']) > 0:
            self.loopingCall.stop()
            self.request.write(self.jsonpcallback+'('+ujson.dumps(response)+')')
            self.request.finish()
            self.finished = True


class DataServer(Resource):
    def getChild(self, sid, request):
        return DataService(sid)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:29:41+00:00Added an answer on May 27, 2026 at 1:29 pm

    When the client navigates away from the page, I can see it continue to loop in the console. I thought the instance would just destroy itself when the client disconnects.

    It doesn’t. First, instances are only destroyed when they are no longer “reachable” (just like any other kind of object you might have).

    When you do this:

    self.loopingCall = task.LoopingCall(self.__print_data)
    

    You’re creating a reference to self.__print_data, which has a reference to self. The LoopingCall then registers itself with the reactor (when you start it). So now the reactor has an indirect reference to self. So self will live forever, or until something changes.

    You can get the LoopingCall to unregister itself from the reactor by stopping it. Once the reactor no longer refers to the LoopingCall, it will no longer be keeping the DataService instance (self) alive.

    If you want to do this when the client closes the connection, then you want to use Request.notifyFinish. This is covered in the Twisted documentation, but it’s pretty straightforward. You should do something like this:

    request.notifyFinish().addErrback(lambda ignored: self.loopingcall.stop())
    

    It’s an errback because you only care about the case where the client closes the connection, not the case where you close the connection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.