Asyncore select statement problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JamesHoward

    #1

    Asyncore select statement problem

    I have a problem with python's asyncore module throwing a bad file
    descriptor error. The code might be difficult to copy here, but the
    problem is essentially:

    The server wants to sever the connection of an open Asyncore socket.
    Calling the socket.close() nor the socket.shutdown (2) calls seem to
    work. The only way I can close the connection without creating the
    error below is to have the client close the connection.

    I have the asyncore.loop() as the last line of a thread that is spawned
    within the applications "mainframe. py" or gui thread. It doesn't seem
    to me like this would make a difference, but I am unfamiliar with the
    specifics of how the asyncore module works.

    Any thoughts people have would be greatly appreciated. If needed I may
    be able to create a small version of the problem to post for people to
    see.

    Thanks,
    Jim Howard


    Exception in thread Thread-1:
    Traceback (most recent call last):
    File
    "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/python2.5/threading.py",
    line 460, in __bootstrap
    self.run()
    File
    "/Users/jwhoward2/Documents/Projects/LJServer/LJDeviceServer/DeviceServer.py ",
    line 23, in run
    asyncore.loop()
    File
    "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/python2.5/asyncore.py",
    line 191, in loop
    poll_fun(timeou t, map)
    File
    "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/python2.5/asyncore.py",
    line 121, in poll
    r, w, e = select.select(r , w, e, timeout)
    error: (9, 'Bad file descriptor')

  • MrJean1

    #2
    Re: Asyncore select statement problem

    Try using another ascyncore example and see if that works for you.
    Maybe, first one without threading, like


    <http://effbot.org/lib/asyncore.html#a syncore-example-basic-http-client>

    Asyncore worked fine on my application on Linux, but haven't tried that
    on MacOS.

    /Jean Brouwers



    JamesHoward wrote:
    I have a problem with python's asyncore module throwing a bad file
    descriptor error. The code might be difficult to copy here, but the
    problem is essentially:
    >
    The server wants to sever the connection of an open Asyncore socket.
    Calling the socket.close() nor the socket.shutdown (2) calls seem to
    work. The only way I can close the connection without creating the
    error below is to have the client close the connection.
    >
    I have the asyncore.loop() as the last line of a thread that is spawned
    within the applications "mainframe. py" or gui thread. It doesn't seem
    to me like this would make a difference, but I am unfamiliar with the
    specifics of how the asyncore module works.
    >
    Any thoughts people have would be greatly appreciated. If needed I may
    be able to create a small version of the problem to post for people to
    see.
    >
    Thanks,
    Jim Howard
    >
    >
    Exception in thread Thread-1:
    Traceback (most recent call last):
    File
    "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/python2.5/threading.py",
    line 460, in __bootstrap
    self.run()
    File
    "/Users/jwhoward2/Documents/Projects/LJServer/LJDeviceServer/DeviceServer.py ",
    line 23, in run
    asyncore.loop()
    File
    "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/python2.5/asyncore.py",
    line 191, in loop
    poll_fun(timeou t, map)
    File
    "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/python2.5/asyncore.py",
    line 121, in poll
    r, w, e = select.select(r , w, e, timeout)
    error: (9, 'Bad file descriptor')

    Comment

    • Gabriel Genellina

      #3
      Re: Asyncore select statement problem

      "JamesHowar d" <James.w.Howard @gmail.comescri bió en el mensaje
      news:1169075120 .491855.103200@ q2g2000cwa.goog legroups.com...
      >I have a problem with python's asyncore module throwing a bad file
      descriptor error. The code might be difficult to copy here, but the
      problem is essentially:
      >
      The server wants to sever the connection of an open Asyncore socket.
      Calling the socket.close() nor the socket.shutdown (2) calls seem to
      work. The only way I can close the connection without creating the
      error below is to have the client close the connection.
      You have to use the dispatcher's close() method, else, the asyncore map
      won't be updated, keeping a reference to the closed socket.

      --
      Gabriel Genellina


      Comment

      • JamesHoward

        #4
        Re: Asyncore select statement problem

        Thank you for the responses. I have learned considerably more about
        how Asyncore works because of it.

        The problem that I see is that Asyncore's poll function does not seem
        to be thread safe. From what I can tell, I am calling
        dispatcher.clos e() properly and the dispatchers are removed from
        asyncore's global map (all except the server itself). However, it
        seems like the error happens when the poll function gets the file
        descriptors to run select on and then the thread ticks and removes them
        from the global map. After this the select call is made, but the file
        descriptors are not valid anymore.

        I guess I have two questions as a result. First, is this a problem
        that anyone else has had and second is there a fix for it? I have
        tried looking for Asyncore thread safe topics in Google, but without
        much luck. If needed I think making the poll function atomic in the
        asyncore module might fix this problem, but I wanted to see what other
        people thought first.

        Thanks again for the help,
        Jim Howard


        Gabriel Genellina wrote:
        "JamesHowar d" <James.w.Howard @gmail.comescri bió en el mensaje
        news:1169075120 .491855.103200@ q2g2000cwa.goog legroups.com...
        >
        I have a problem with python's asyncore module throwing a bad file
        descriptor error. The code might be difficult to copy here, but the
        problem is essentially:

        The server wants to sever the connection of an open Asyncore socket.
        Calling the socket.close() nor the socket.shutdown (2) calls seem to
        work. The only way I can close the connection without creating the
        error below is to have the client close the connection.
        >
        You have to use the dispatcher's close() method, else, the asyncore map
        won't be updated, keeping a reference to the closed socket.

        --
        Gabriel Genellina

        Comment

        • Gabriel Genellina

          #5
          Re: Asyncore select statement problem

          At Friday 19/1/2007 14:42, JamesHoward wrote:
          >Thank you for the responses. I have learned considerably more about
          >how Asyncore works because of it.
          >
          >The problem that I see is that Asyncore's poll function does not seem
          >to be thread safe. From what I can tell, I am calling
          >dispatcher.clo se() properly and the dispatchers are removed from
          >asyncore's global map (all except the server itself). However, it
          >seems like the error happens when the poll function gets the file
          >descriptors to run select on and then the thread ticks and removes them
          >from the global map. After this the select call is made, but the file
          >descriptors are not valid anymore.
          >
          >I guess I have two questions as a result. First, is this a problem
          >that anyone else has had and second is there a fix for it? I have
          >tried looking for Asyncore thread safe topics in Google, but without
          >much luck. If needed I think making the poll function atomic in the
          >asyncore module might fix this problem, but I wanted to see what other
          >people thought first.
          Usually asyncore is used with only one thread - that's one of the
          reasons of using it btw. You can spawn other working threads, but
          they don't usually interact with the networking stuff directly.


          --
          Gabriel Genellina
          Softlab SRL






          _______________ _______________ _______________ _____
          Preguntá. Respondé. Descubrí.
          Todo lo que querías saber, y lo que ni imaginabas,
          está en Yahoo! Respuestas (Beta).
          ¡Probalo ya!


          Comment

          • JamesHoward

            #6
            Re: Asyncore select statement problem

            Again, thank you for your help. With digging through the Asyncore.py
            source, I was able to find the poll2 function which is called when the
            function asyncore.loop(u se_poll = True) is enabled.

            This function does not use a select call, but a poll call to do its
            looping. It works well for the problem of threads closing devices at
            unknown times. The reason for this is that the select statement is
            called on a series of file descriptors that should not be changed. If
            the file descriptors become invalid, select throws and exception and
            the asyncore loop haults. The use_poll flag sets the asyncore module
            to use a poll instead of a select statement.

            Within the poll method, there are better ways of dealing with file
            descriptors and they seem to be able to discern if a file descriptor
            becomes disconnected with the POLLHUP flag.

            I am still unsure as to why the select function is used instead of the
            poll function, but using poll appears to have solved my problem.

            Thanks for all the help,
            Jim Howard

            Comment

            Working...