process/thread instances and attributes

Jesse Noller jnoller at gmail.com
Fri Jan 16 07:33:34 EST 2009


On Thu, Jan 15, 2009 at 10:37 PM, James Mills
<prologic at shortcircuit.net.au> wrote:
> After some work ... I've taken Laszlo's suggestion of using Value
> (shared memory) objects
> to share state between the -pseudo- Process (manager) object and it's
> underlying multiprocessing.Process
> instance (and subsequent process):
>
> Here is the code:
>
> ----------------------------------------
> #!/usr/bin/env python
>
> import os
> from time import sleep
>
> from threading import activeCount as threads
> from threading import Thread as _Thread
>
> from multiprocessing import Value
> from multiprocessing import Process as _Process
> from multiprocessing import active_children as processes
>
> class Process(object):
>
>    def __init__(self, *args, **kwargs):
>        super(Process, self).__init__(*args, **kwargs)
>
>        self.running = Value("b", False)
>        self.thread = _Thread(target=self.run)
>        self.process = _Process(target=self._run, args=(self.running,))
>
>    def _run(self, running):
>        self.thread.start()
>
>        try:
>            while running.value:
>                try:
>                    sleep(1)
>                    print "!"
>                except SystemExit:
>                    running.acquire()
>                    running.value = False
>                    running.release()
>                    break
>                except KeyboardInterrupt:
>                    running.acquire()
>                    running.value = False
>                    running.release()
>                    break
>        finally:
>            running.acquire()
>            running.value = False
>            running.release()
>            self.thread.join()
>
>    def start(self):
>        self.running.acquire()
>        self.running.value = True
>        self.running.release()
>        self.process.start()
>
>    def run(self):
>        pass
>
>    def stop(self):
>        print "%s: Stopping ..." % self
>        self.running.acquire()
>        self.running.value = False
>        self.running.release()
>
>    def isAlive(self):
>        return self.running.value
>
> class A(Process):
>
>    def run(self):
>        while self.isAlive():
>            sleep(5)
>            self.stop()
>
> a = A()
> a.start()
>
> N = 0
>
> while a.isAlive():
>    sleep(1)
>    print "."
>    print "threads:   %d" % threads()
>    print "processes: %d" % len(processes())
>
> print "DONE"
> ----------------------------------------
>
> Here is the result of running this:
>
> ----------------------------------------
> $ python test3.py
> !
> .
> threads:   1
> processes: 1
> .
> threads:   1
> processes: 1
> !
> !
> .
> threads:   1
> processes: 1
> .
> threads:   1
> processes: 1
> !
> .
> threads:   1
> processes: 1
> !
> <__main__.A object at 0x80de42c>: Stopping ...
> !
> .
> threads:   1
> processes: 0
> DONE
> ----------------------------------------
>
> This appears to work as I intended.
>
> Thoughts / Comments ?
>
> cheers
> James


Personally, rather then using a value to indicate whether to run or
not, I would tend to use an event to coordinate start/stop state.

-jesse



More information about the Python-list mailing list