473,548 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python Asynchronous Services

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 threadingTCPSer ver, 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 .....
Jul 18 '05 #1
2 3359
On Thu, 17 Jul 2003, "Graeme Matthew" <gs*******@ozem ail.com.au> 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.sta tic.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/

Jul 18 '05 #2
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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
8658
by: Michael Lindsey | last post by:
I need to write a server app to send images to client GUIs that are outside of the server's domain. The client will have the file system path to the image but can not access the file system. I am trying to decide if I should use remoting vs. writing a server that uses networkstreams. I have read that networkstreams\tcp programming should...
8
2297
by: Trotsky | last post by:
Hi I have asked a similar question on the web services discussion group, but the question is a bit more related to ASP.Net. Basically I have a ASP.Net application that calls a web service asynchronously. This is fine. My problem is that when I make a call to the web service asynchronously when I get the return which is passed into a static method...
1
3162
by: org | last post by:
Hi, I'm developing a web service with should be used by an .NET CF2 client and an .NET 2.0 Windows client. I've tried to put all the connection logic into one class, which could be used in common from the mobile and the windows client. But this didn't work: The new .NET framework supports asynchronous web services with the...
1
2055
by: Ron Davis | last post by:
I have recently discovered Python and like it quite a bit. I would like to use it on a new project I am starting. The project will gather data from several web services and present the collected data to browser users through a web server. So basically I need a full-time web server and a separate way to poll web services for their data....
3
1346
by: VMI | last post by:
I posted this question several days ago, but I don't think I specified what version of .Net I was using. I'm currently using VS 2005 framework 2.0. Where can I find information on asynchronous web service calls for VS 2005? I've found some articles, but they apply to VS 2003. I wrote a web service that processes a file and stores it into a db....
4
1740
by: Redefined Horizons | last post by:
I've been hearing a ot about AJAX lately. I may have to build a web application in the near future, and I was curoius: How does a web application that uses Python compare with one that uses AJAX? I've done some basic web page design with HTML and CSS, but never any web applications. I don't want to learn a new language if I can use...
0
1618
by: Raymondr | last post by:
Hi, First a brief description of out application: We have a webapplication which calls a couple of webservices during one request (postback). These calls to the webservices are made concurrent using asynchronous webservices calls. The number of webservices called concurrent is between 1 and 18. The webservice calls are made using SSL with...
8
1722
by: Simon Gorski | last post by:
I have a large problem, and I believe there is not yet a way to solve this using IIS and ASP.NET. I hope someone has a solution which we couldn't find. The current situation When a user logs in to our website, we implement a single login for multiple services (let's call them services A-D). This means, that on the backend, while...
4
4068
by: Morgan Cheng | last post by:
Since ASP.NET 2.0, asynchronous web service client can be implemented with event-based pattern, instead of original BeginXXX/EndXXX pattern. However, I didn't find any material about event-based server side asynchronous web service. So, we can only implement asynchronous webmethod with BeginXXX/EndXXX pattern, right? I don't why ASP.NET 2.0...
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.