Python Asynchronous Services

Discussion in 'Python' started by Graeme Matthew, Jul 17, 2003.

  1. Hi all, I have hit a serious snag ...

    I am writing an app server. I am happy with pythons threading etc however I
    am totally confused with handling large request on a socket. For example I
    can create a threadingTCPServer, the script that calls the app server might
    send a file or an xml request. Where I am stuck is how do you handle large
    files ?

    for example:

    while 1:
    data = recv(1024)
    if not data: break

    surely in a thread this will block all other threads until all data is
    recieved. I have also been told that one cannot rely on this mechanism as
    the socket might end up in a continious loop, i.e you need to send a header
    included in the data that states the length (bytes) that is been sent, or
    you need some terminator, what happens if the terminator is within the
    requests data and not at the end ?

    please any help or examples as I have now been on this for 2 days without
    any luck. I have looked at asyncore and asynchat, problem is once the server
    socket input is completed then their will be an area that is CPU bound where
    it will need to call the model, database etc and produce the html request,
    this means that control will only be handed back to IO operations when this
    is finished, effectively blocking all other requests ....

    I suppose another question is can python handle large files and requests at
    the same time or is one better off simply forking the process even though
    its costly ?

    Many thanks .....
     
    Graeme Matthew, Jul 17, 2003
    #1
    1. Advertising

  2. Graeme Matthew

    Moshe Zadka Guest

    On Thu, 17 Jul 2003, "Graeme Matthew" <> wrote:

    > Hi all, I have hit a serious snag ...

    ....
    > please any help or examples as I have now been on this for 2 days without
    > any luck. I have looked at asyncore and asynchat, problem is once the server
    > socket input is completed then their will be an area that is CPU bound where
    > it will need to call the model, database etc and produce the html request,
    > this means that control will only be handed back to IO operations when this
    > is finished, effectively blocking all other requests ....


    Well, I can recommend Twisted. Twisted uses a producer/consumer model
    to send large file, which is fairly transparent [there is a good
    cut'n'pastable example of usage of this in twisted.web.static.File].
    This should be enough to handle the network part. You claim there
    is a CPU-bound operation after that -- if this is really true [and
    in my experience, in many of the cases the operation are short enough
    that it is not required] -- you can use Twisted's deferToThread to
    treat those operations as though they were completely asynchronous.
    This uses Twisted's internal threadpool support.

    More information: http://twistedmatrix.com/
    [Disclaimer: I am part of the Twisted developement team]
    --
    Moshe Zadka -- http://moshez.org/
    Buffy: I don't like you hanging out with someone that... short.
    Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
    Agile Programming Language -- http://www.python.org/
     
    Moshe Zadka, Jul 17, 2003
    #2
    1. Advertising

  3. Graeme Matthew

    Peter Hansen Guest

    Graeme Matthew wrote:
    >
    > while 1:
    > data = recv(1024)
    > if not data: break
    >
    > surely in a thread this will block all other threads until all data is
    > recieved.


    Why "surely"? It's actually not the case. A blocking call such as to
    socket.recv() means that the *calling* thread is blocked until the call
    returns, not that other threads are blocked. In fact, the other threads
    will immediately be free to run since the calling thread will release
    the Global Interpreter Lock before it blocks in the OS socket call.

    > I have also been told that one cannot rely on this mechanism as
    > the socket might end up in a continious loop, i.e you need to send a header
    > included in the data that states the length (bytes) that is been sent, or
    > you need some terminator, what happens if the terminator is within the
    > requests data and not at the end ?


    Now you're getting into a higher level. You certainly *can* "rely" on
    sockets, or receiving data in chunks like the above, but it's certainly
    not the easiest way to approach things at this point. You know about
    asyncore, and it would work, and Moshe has mentioned Twisted (and I have
    to agree that looking there is your best bet); no point reinventing the
    wheel, and even if you're trying to learn, you'll probably learn a lot
    by examining the source to either of those packages.

    > please any help or examples as I have now been on this for 2 days without
    > any luck. I have looked at asyncore and asynchat, problem is once the server
    > socket input is completed then their will be an area that is CPU bound where
    > it will need to call the model, database etc and produce the html request,
    > this means that control will only be handed back to IO operations when this
    > is finished, effectively blocking all other requests ....


    Knowing about how to use a Queue to communicate with a pool of threads, or
    a single worker thread, for CPU bound requests, is a good skill...

    > I suppose another question is can python handle large files and requests at
    > the same time or is one better off simply forking the process even though
    > its costly ?


    Python is quite capable of this... and forking is definitely not required.
    On the other hand, you're better off in general not worrying about performance
    issues until you've figured out how to make something work properly. Forking
    is not the best approach, but mainly because of its relative complexity, not
    because of "cost".

    -Peter
     
    Peter Hansen, Jul 17, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary Howlett

    Asynchronous Web Services

    Gary Howlett, Aug 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    446
    Natty Gur
    Aug 19, 2003
  2. Dave
    Replies:
    0
    Views:
    138
  3. Bill Zack
    Replies:
    4
    Views:
    148
  4. David Cornelson

    Asynchronous Web Services called from ASP.NET

    David Cornelson, Dec 8, 2003, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    156
    Ágúst
    Dec 9, 2003
  5. sing

    Asynchronous Web Services

    sing, Feb 16, 2004, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    179
    Sami Vaaraniemi
    Feb 17, 2004
Loading...

Share This Page