is it safe to stop a thread?

Parzival Herzog parz at home.com
Wed May 23 16:08:31 EDT 2001


"Eric Lee Green" <eric at badtux.org> wrote in message news:slrn9gmlc6.tpc.eric at edesk.inhouse...

> thread a grabs the semaphor protecting the global socket.
> thread b kills thread a.
> thread c tries to grab the semaphor protecting the global socket.
>
> *DEADLOCK!*.

Don't want this.  Want:

---Thread a:
    try:
      try:
        lock.acquire()
        doSomething()
      finally:
        lock.release()
    except thread.alerted, whatHappened:
      print "I goofed:", whatHappened
      sys.exit()

---Thread b:

    thread_a.alert("You forgot your keys, you silly silly goose.")

---Thread c:
    try:
        lock.acquire()
        doSomethingElse()
    finally:
        lock.release()

> Threads are evil. They are evil, wicked, nasty things. They are evil,
> wicked nasty things because there's no easy way to free the resources
> that a thread has allocated if the thread dies or is killed, unless


If thread are so evil, what are they doing in Python? Ruining fair maidens?
Shame!

> you do some very tedious book-keeping. I thought doing that tedious
> book-keeping was why operating systems were invented, especially the
> whole notion of a "process", where the allocated resources are
> automagically de-allocated when the process dies.

Actually OS processes are not that good at terminating resources, witness
the files whose buffers are not flushed, then network connections
that hang...

IMHO, try - finally is what is good for freeing resources with a minimum of
tedium.

I believe processes were invented to have support separate programs
running in separate address spaces, so that 1) they could be time-multiplexed,
and 2) while running "simultaneously", they would be isolated from each
other (i.e the OS could guarantee that a process could continue running
no matter what evil or ignorant deeds were done by other processes.

The unix tradition of using multiple processes within the same program
is a horrifically cumbersome kludge, not a convenience, and threads were
invented to get us out of that kludge, by removing the complexities of
communicating across separate address spaces within the same program.

- argumentative-ly yours,
    Parzival






More information about the Python-list mailing list