472,780 Members | 1,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

Bug? asyncore.dispatcher_with_send trouble

Hi,

I've been mangling python-irclib into an asyncore class, so it fits in
nicely with the rest of my app. I ran into a problem with
asyncore.dispatcher_with_send (Python 2.3.4), though. Not sure if this
is the right place to file a bug, but here goes:
class dispatcher_with_send(dispatcher):

def __init__(self, sock=None):
dispatcher.__init__(self, sock)
self.out_buffer = ''

def initiate_send(self):
num_sent = 0
num_sent = dispatcher.send(self, self.out_buffer[:512])
self.out_buffer = self.out_buffer[num_sent:]

def handle_write(self):
self.initiate_send()

def writable(self):
return (not self.connected) or len(self.out_buffer)

def send(self, data):
if self.debug:
self.log_info('sending %s' % repr(data))
self.out_buffer = self.out_buffer + data
self.initiate_send()
I assumed that disp.send('chunkofdata') would merely put it in the out
buffer until the socket is writable, but this isn't what happens. It
tries to send() the data straight away, which can raise an exception...
and since we're not inside the asyncore.write() function, handle_error
is never called.

Here's the fixed version that I've cobbled together:
class buffered_dispatcher(asyncore.dispatcher):
def __init__(self, sock=None):
asyncore.dispatcher.__init__(self, sock)
self.out_buffer = ''

# We only want to be writable if we're connecting, or something is
in our
# buffer.
def writable(self):
return (not self.connected) or len(self.out_buffer)

# Send some data from our buffer when we can write
def handle_write(self):
sent = asyncore.dispatcher.send(self, self.out_buffer)
self.out_buffer = self.out_buffer[sent:]

# We want buffered output, duh
def send(self, data):
self.out_buffer += data
Hopefully this saves someone else half an hour of annoyance at some point :)

Freddie
Jul 18 '05 #1
2 2529
Freddie wrote:
Hi,

I've been mangling python-irclib into an asyncore class, so it fits in
nicely with the rest of my app. I ran into a problem with
asyncore.dispatcher_with_send (Python 2.3.4), though. Not sure if this
is the right place to file a bug, but here goes:
No, it isn't. The right place to file a bug would be

http://sourceforge.net/tracker/?grou...70&atid=105470

class dispatcher_with_send(dispatcher):

def __init__(self, sock=None):
dispatcher.__init__(self, sock)
self.out_buffer = ''

def initiate_send(self):
num_sent = 0
num_sent = dispatcher.send(self, self.out_buffer[:512])
self.out_buffer = self.out_buffer[num_sent:]

def handle_write(self):
self.initiate_send()

def writable(self):
return (not self.connected) or len(self.out_buffer)

def send(self, data):
if self.debug:
self.log_info('sending %s' % repr(data))
self.out_buffer = self.out_buffer + data
self.initiate_send()
I assumed that disp.send('chunkofdata') would merely put it in the out
buffer until the socket is writable, but this isn't what happens. It
tries to send() the data straight away, which can raise an exception...
and since we're not inside the asyncore.write() function, handle_error
is never called.

Here's the fixed version that I've cobbled together:
class buffered_dispatcher(asyncore.dispatcher):
def __init__(self, sock=None):
asyncore.dispatcher.__init__(self, sock)
self.out_buffer = ''

# We only want to be writable if we're connecting, or something is
in our
# buffer.
def writable(self):
return (not self.connected) or len(self.out_buffer)

# Send some data from our buffer when we can write
def handle_write(self):
sent = asyncore.dispatcher.send(self, self.out_buffer)
self.out_buffer = self.out_buffer[sent:]

# We want buffered output, duh
def send(self, data):
self.out_buffer += data
Hopefully this saves someone else half an hour of annoyance at some
point :)

Freddie


I haven't actually studied your code in detail, but I have trouble
understanding how something so apparently reliable as asyncore is
suddenly revealed to have fatal flaws in it. Sam Rushing is a pretty
experienced programmer.

Under what circumstances does calling .send(data) result in exceptions
being raised?

regards
Steve
Jul 18 '05 #2
Steve Holden wrote:
Freddie wrote:
Hi,

I've been mangling python-irclib into an asyncore class, so it fits in
nicely with the rest of my app. I ran into a problem with
asyncore.dispatcher_with_send (Python 2.3.4), though. Not sure if this
is the right place to file a bug, but here goes:
No, it isn't. The right place to file a bug would be

http://sourceforge.net/tracker/?grou...70&atid=105470


Ah. Thanks.

<snip>

I haven't actually studied your code in detail, but I have trouble
understanding how something so apparently reliable as asyncore is
suddenly revealed to have fatal flaws in it. Sam Rushing is a pretty
experienced programmer.

Under what circumstances does calling .send(data) result in exceptions
being raised?

regards
Steve


I'm actually being slightly crazy and using a single dispatcher for the
entire 'life' of that IRC connection. It can be connected/disconnected
multiple times over it's life. Calling send() can sometimes happen when
the socket is closed/broken/whatever, so calling initiate_send()
straight away in there can and will blow up... and it's outside the
normal handle_error() exception trap (read/write methods in
asyncore.py). This is not how I thought a 'buffered output' dispatcher
would work. Having a 'buffered' send() that can actually block, or even
raise an exception that you have to catch yourself (instead of
handle_error being called like every other exception) seems broken to me :|

Freddie
Jul 18 '05 #3

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

Similar topics

0
by: Michael Welsh | last post by:
In order to learn sockets in Python I am trying to write a simple group chat server and client. I need a little nudge, please. My question contains some GUI but only as decoration. The root...
5
by: george.trojan | last post by:
My application consists of Tkinter GUI that has to communicate with a remote server. The communication is bi-directional: the GUI responds to remote requests and user actions uch as pressing a...
5
by: David M. Wilson | last post by:
Hi peeps, I finally got around recently to doing something serious using Python for serving network clients asynchronously, deciding on asyncore as my starting point. After 2 days or so of...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
0
by: Tony Meyer | last post by:
Changes in asyncore from 2.3 to 2.4 mean that asyncore.poll() now passes all the sockets in the map to select.select() to be checked for errors, which is probably a good thing. If an error occurs,...
7
by: billie | last post by:
Hi all. I've just terminated a server application using asyncore / asynchat frameworks. I wrote a test script that performs a lot of connections to the server app and I discovered that asyncore...
7
by: Paul Kozik | last post by:
I am working on the networking code for a small Multiplayer RPG I'm working on. I currently have some basic code using threads, but it seems like asyncore would be far better suited for my needs....
0
by: Giampaolo Rodola' | last post by:
Hi, I post this message here in the hope someone using asyncore could review this. Since the thing I miss mostly in asyncore is a system for calling a function after a certain amount of time, I...
8
by: Frank Millman | last post by:
Hi all I have been using my own home-brewed client/server technique for a while, using socket and select. It seems to work ok. The server can handle multiple clients. It does this by creating a...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.