Stopping threads from the outside

Boudewijn Rempt boud at valdyas.org
Sun Mar 24 16:18:33 EST 2002


Peter Hansen wrote:

> holger at trillke.net wrote:
>> 
>> > >how could you stop a running threading.Thread from the *outside*? of
>> > >course i could introduce some kind of flag into the event loop of the
>> > >Thread.run-method, but i really would prefer to have a non-instrusive
>> > >way for several reasons.
>> > >
>> > >Any way to do this?
>> >
>> > Not really.  Why do you want to do it?
>> 
>> You really want to know it? I am currently working
>> on (coded) agents which move through the network.
> 
> "The network"?  Which one?  The Internet, or an internal
> network over which you have full control?  Who controls
> the servers on which these agents will be running?
> 
>> But of course i'd sometimes like to shut down some or
>> all agent-threads.
> 
> Run them in a separate process and kill the process when
> it's "sometime"?  If you can live without individual
> control.  Maybe you can identify groups of them?
> 

Actually, Holger's problem is very well known to people
implementing agents. Agents run within the application, but
they don't share objects (not often, anyway), so killing them
is safe. But neither in Java nor in Python you can easily kill
them.

Running each agent in its own process makes the system very 
costly in terms of resources, makes it necessary for the agents
to implement inter-process messaging. Not to mention them
showing up individually in _top_...

>> Is the questions actually strange in any way? Like something
>> you wouldn't usually like to do?
> 
> Yes, that's the case.  It's generally considered to lead to
> undefined behaviour if you do manage to kill a thread from
> the outside, because of resource locking issues and such.
> 

That's the received opinion indeed. It still leads to problems.
I mean -- it's why you can kill a process, but not a thread. But in
an agent system, every agent (presumably running a thread, although
I've implemented systems where agents share threads, cooperative
multi-tasking agents) is on its own, and shouldn't own objects
or locks.

> Maybe you can get by with Stackless Python?  The microthreads
> provided there ought to be more easily killable, though
> I haven't tried it myself.

They aren't -- not really. I tried myself. See my article on
Stackless at InformIT.

-- 
Boudewijn Rempt | http://www.valdyas.org



More information about the Python-list mailing list