process/thread instances and attributes

Discussion in 'Python' started by James Mills, Jan 15, 2009.

  1. James Mills

    James Mills Guest

    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"
    James Mills, Jan 15, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Volker Raum

    instances ant staic attributes

    Volker Raum, Aug 28, 2003, in forum: Java
    Replies:
    8
    Views:
    385
    Thomas G. Marshall
    Aug 29, 2003
  2. John Wohlbier
    Replies:
    2
    Views:
    366
    Josiah Carlson
    Feb 22, 2004
  3. Pedro Werneck
    Replies:
    4
    Views:
    428
    Pedro Werneck
    Aug 8, 2006
  4. Replies:
    8
    Views:
    459
    James Stroud
    Jan 29, 2009
  5. Jason Kendall
    Replies:
    1
    Views:
    140
    Patrice
    Jun 1, 2005
Loading...

Share This Page