Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   process/thread instances and attributes (http://www.velocityreviews.com/forums/t665556-process-thread-instances-and-attributes.html)

James Mills 01-15-2009 01:53 AM

process/thread instances and attributes
 
Hey all,

I have this concept I'm working on and here is
the code... Problem is if you run this it doesn't
terminate. I believe you can terminate it in the
main process by calling a.stop() But I can't find a
way for it to self terminate, ie: self.stop() As indicated
by the code...

----------------------------------------

#!/usr/bin/env python

import os
from time import sleep

from threading import Thread as _Thread
from multiprocessing import Process as _Process

class Process(object):

def __init__(self, *args, **kwargs):
super(Process, self).__init__(*args, **kwargs)

self.running = False
self.process = _Process(target=self._run)

def _run(self):
thread = _Thread(target=self.run)
thread.start()

try:
while self.running and thread.isAlive():
try:
sleep(1)
print "!"
except SystemExit:
self.running = False
break
except KeyboardInterrupt:
self.running = False
break
finally:
thread.join()

def start(self):
self.running = True
self.process.start()

def run(self):
pass

def stop(self):
print "%s: Stopping ..." % self
self.running = False

class A(Process):

def run(self):
while self.running:
sleep(5)
self.stop()
a = A()
a.start()

while a.running:
sleep(1)
print "."

print "DONE"

----------------------------------------

cheers
James

--
-- "Problems are solved by method"


All times are GMT. The time now is 07:25 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.