Embedded python: how to force a break to endless looping Python script? (2)

Thomas Wouters thomas at xs4all.net
Tue Aug 22 17:48:23 EDT 2000


On Tue, Aug 22, 2000 at 02:37:43PM -0400, Warren Postma wrote:
> Okay, that wasn't clear at all. Here's another go at it:

[ Warren exlains he needs other threads to run arbitrary code, and be able
to break into them ]

> Has anyone done anything like this?

I have wanted this for a fair bit of time, for embedding Python in MUSH
(MUX, actually, but close enough) and giving people access to Python from
inside (kind of like what MOO does, for those who know it.) But you don't
want to do that if that means that one person doing 'while 1: pass' can hang
the MUX (or at least the Python part. Or even their own connection, eating
up system resources forever.)

But, while hacking Python internals (great fun, I must say!) I found a way
to do this! It's bad for performance, though: you set a trace function (a
global one, via sys.settrace()) that does some basic checks (like whether
the thread should die already) and possibly returns a new trace function (or
just itself) if you want to trace each line of code, which would do the same
kind of checks.

How bad a performance hit it is depends on how fine a control you want. It's
possible to loop forever without calling a function, in which case you need
the 'local' trace function, but if you don't care about that situation, a
global tracefunc should be enough (just make it return 'None' so no local
tracefunc gets set.)

There's some basic info on trace functions and some pointers to Frame
objects (which is what the trace function gets passed in) in the libary
refrence, in the debugger module (pdb).

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list