Blocked thread

Chris Angelico rosuav at gmail.com
Thu Jul 17 20:19:17 CEST 2014


On Fri, Jul 18, 2014 at 4:13 AM, Larry Martell <larry.martell at gmail.com> wrote:
> But every once it a while it takes 2 minutes. It will be hard
> to detect that manually. What I was trying to do in my threaded code
> was detect when it was taking more than 5 seconds, and then start
> monitoring it.

Fair enough. In that case, I'd just do it the simple way: get the
backend PID (or whatever other connection identification is
appropriate), spin off a thread/process to do the monitoring. It
sleeps five seconds, then does its first query. If the server says
that connection's done its work, easy! Disconnect, job done.
Otherwise, do whatever monitoring you can.

But if your main goal is diagnosis, rather than monitoring, here's a
really really naughty idea that might help: Refrain from committing if
it's taken too long. Something like this:

start = time.time()
# do the actual work, but don't commit
if time.time() > start + 5:
    # raise an alarm, tell a human being that something's wrong
else:
    commit()

Obviously you run this "dangeroussed-up version" of the script only
when there's a person there to react to the alarm, otherwise you go
for the normal one. But this could give you a massive insight into
what's happening, because until you commit, you have all your locks
and things.

ChrisA



More information about the Python-list mailing list