Bug#656137: readme.txt: asynchronous usage is a misleading lie
Enrico Zini
enrico at debian.org
Tue Jan 17 14:21:05 UTC 2012
On Mon, Jan 16, 2012 at 08:53:06PM +0100, Enrico Zini wrote:
> - Actions are synchronous. After sending a command to asterisk, a
> readline() is issued right away. Again, this blocks the event loop
> until asterisk replies, if it ever does.
This is in fact a bigger issue than just documentation. Suppose you want
to detect if the network connection to asterisk went down, and do
something like this:
class ResilientManager(Asterisk.Manager):
[...]
def read_loop(self, idle_timeout=5*60.0):
"""
Read loop that will quit if there has been no input from asterisk for a
configurable amount of seconds
"""
while True:
rlist, wlist, xlist = select.select([self], [], [], idle_timeout)
if not rlist:
# It timed out
return
else:
self.read()
def read_loop_with_pings(self, idle_timeout=5*60.0):
"""
Read loop that, if nothing is heard from Asterisk after a given idle
timeout, performs a ping to see if Asterks is still there.
"""
while True:
self.read_loop(idle_timeout)
log.info("%s: no news from asterisk after %f seconds: pinging", self.name, idle_timeout)
event = self.Ping()
log.info("%s: asterisk replied to ping", self.name)
In this case, since Ping reads its reply with a readline(), if the
network went down then readline() waits indefinitely, defeating the
purpose of doing a Ping in the first place. I tested it here,
disconnecting the VPN to the asterisk server: a Ping was issued by the
process has been stuck in the Ping() method since 15 minutes and
counting.
At least for Ping, you *definitely* need at least a timeout.
As a workaround, one needs to wrap the above code in a
multiprocessing.Process, and then implement a further watchdog in the
parent process.
Ciao,
Enrico
--
GPG key: 4096R/E7AD5568 2009-05-08 Enrico Zini <enrico at enricozini.org>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
URL: <http://lists.alioth.debian.org/pipermail/pkg-voip-maintainers/attachments/20120117/5c52babd/attachment.pgp>
More information about the Pkg-voip-maintainers
mailing list