문제

Is there "standard" or well known class that do HTTP Client using asyncore.dispatcher ?

I can do one, but I prefer not to invent the wheel again :)

도움이 되었습니까?

해결책

The short answer is: no, at least not anything you'd want to use.

Most people believe that asyncore is somewhat useful as "sample code" on how to write a select-based event loop, but not useful as a framework for real-life projects beyond trivial complexity.

There have been a few projects in the past to build serious things on top of asyncore, including at least one web server, but I don't know of a web client. That doesn't mean that no such thing exists… but I wouldn't expect it to be well-maintained or heavily-used.

Instead of building higher-level frameworks on top of asyncore, people have mostly focused on building new frameworks from scratch. The most popular are probably twisted (which has a vaguely similar architecture to asyncore, but much more powerful and flexible) and gevent (which is very different—it lets you write code in the synchronous/multithreaded style and it magically runs via greenlets in a single thread around an event loop).

Meanwhile, Guido and friends are designing a new, modern async library (see PEP 3146 and tulip for details) that will probably appear in 3.4, and may deprecate asyncore.

So, your choices are:

  • Build it yourself out of httplib and asyncore.
  • Search for an old, partially-finished implementation and finish it.
  • Use twisted, gevent, tulip, etc.
  • Use a different high-level abstraction like pycurl (or any of the other libraries that wrap libcurl and its curl_multi API) or grequests (which asynchronizes the very high-level requests HTTP client library using gevent).

Of course it's also worth considering whether you really need a single-threaded event loop. Most client-side apps don't need to handle more than a dozen or so connections, and a thread per socket can easily handle that on any platform. (Of course Python threads suck if you have any CPU-bound code, but single-threaded event-loop-based code sucks even worse for that, so I'll assume that isn't a problem.)


First, a couple general comments on your comments:

I'm not sure what you mean by "performance". Normally, with networking apps, you worry about scalability: How many simultaneous connections can you handle, how many new connections you can make per second, how much throughput you can sustain, etc. Performance—usually measured in terms of how much CPU/power-draw/heat-dissipation you're using during a steady-state workflow—usually only comes into it once you've got all the scalability you need, and even then only if you're planning on running dozens of rooms stacked with boxes running your server.

At any rate, I have no idea what you're testing, but all of these options demonstrably outpace asyncore on Linux and Mac OS X/*BSD, and completely blow it away on Windows, under any reasonable test anyone's ever done. They'll use IOCP, epoll, or kqueue instead of just poll. Some of them also put the central code in C. They're all being extensively tested and tuned under heavy real-world usage, while asyncore hasn't had any improvements since poll support was added more than a decade ago. (And that's not because asyncore is already good enough, but because it's not good enough to be worth improving.)

But for client-side apps, neither scalability nor performance is usually an issue. Just doing parallel downloads from a couple dozen sites is usually more than enough to saturate your FTTH/cable/DSL/etc. bandwidth, and beyond that, what more do you want?

So, the reason I suggested these alternatives was not for performance, or scalability, but for simplicity and completeness. Twisted's HTTP client or grequests means you only need to write 5 lines of code, and you have something that works, and all the hard parts are already thought through and heavily tested. Even building it yourself, it takes a whole lot less work (and offers a whole lot less opportunity for bugs) to run httplib/urllib2/whatever on top of gevents than to tear it apart and rebuild it around asyncore instead of blocking sockets.

twisted - i am beginner, and dont have that much time to invest, at least for the moment.

While twisted is obviously a huge project, you don't have to learn all of it, and it should be much easier to use for a beginner than asyncore. Look at the HTTP client example in the link I gave earlier. Compare that to how much code it would take to do the same thing with httplib and asyncore.

gevent - performance is similar to asyncore, but server load is way to high.

If server load is way too high, that can only mean your client is running way too fast… And that's a good problem to have; you can always throttle back a too-fast client, but you generally can't do much with a too-slow client except rewrite the whole thing.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top