How best to achieve asynchronous socket input and output?

K

Kylotan

I'm writing a wxPython telnet-like application that needs to be able
to send and receive data independently of each other. The asyncore
library comes close to what I want, but still requires I have a
blocked thread (the one that calls asyncore.loop() ). It also seems a
little unpredictable; one example of this is that when I based an
example around the sample HTTP client (11.23.1 in the docs),
handle_write never seemed to get called, and I had to call .send()
directly, bypassing any buffering.

Is there a better way of achieving what I want? Or something I need to
know about the asyncore library to get it to do this?
 
A

Albert Hofkamp

I'm writing a wxPython telnet-like application that needs to be able
to send and receive data independently of each other. The asyncore
library comes close to what I want, but still requires I have a
blocked thread (the one that calls asyncore.loop() ). It also seems a
little unpredictable; one example of this is that when I based an
example around the sample HTTP client (11.23.1 in the docs),
handle_write never seemed to get called, and I had to call .send()
directly, bypassing any buffering.

Is there a better way of achieving what I want? Or something I need to
know about the asyncore library to get it to do this?

I usually jump immediately to select, which maps almost directly to select(2),
giving a lot of control, and lack of portability for non-unix systems.

In the context of GUI's, maybe you can get the eventloop of wxpython to
wait for file I/O on the sockets, some GUI libraries provide such a hook
(GUI handling in the end means waiting for data from the X11 server, ie
a select-like thing).
Otherwise you must poll, or use a seperate thread.
 
A

Andrew Bennetts

I'm writing a wxPython telnet-like application that needs to be able
to send and receive data independently of each other. The asyncore
library comes close to what I want, but still requires I have a
blocked thread (the one that calls asyncore.loop() ). It also seems a
little unpredictable; one example of this is that when I based an
example around the sample HTTP client (11.23.1 in the docs),
handle_write never seemed to get called, and I had to call .send()
directly, bypassing any buffering.

Is there a better way of achieving what I want? Or something I need to
know about the asyncore library to get it to do this?

You can use Twisted to do this, but unfortunately its builtin wxpython
support isn't very good. There is however a workaround for it:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/181780

(There's also a more comprehensive example app here:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/203471)

If you don't want to switch to Twisted, perhaps a similar approach would
work for asyncore?

-Andrew.
 
F

Fredrik Lundh

Kylotan said:
I'm writing a wxPython telnet-like application that needs to be able
to send and receive data independently of each other. The asyncore
library comes close to what I want, but still requires I have a
blocked thread (the one that calls asyncore.loop() ).

using a timer to poll asyncore tends to work pretty well.

for examples, see "Managing Downloads" and "Keeping the network
traffic going" on this page:

http://effbot.org/zone/effnews-3.htm

(you can search for "poll" to find the relevant portions)

</F>
 
K

Kylotan

Fredrik Lundh said:
using a timer to poll asyncore tends to work pretty well.

for examples, see "Managing Downloads" and "Keeping the network
traffic going" on this page:

http://effbot.org/zone/effnews-3.htm

(you can search for "poll" to find the relevant portions)

Ok, so from what you're saying, and what the others in the thread have
said, it seems like I can't get around having to poll the sockets
periodically somehow, in another thread. I guess I will just make a
thread for all my sockets and pass it relevant callbacks for whenever
select decides that a socket has some data incoming.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top