6

I have a module called randomstuff which I import into my main program. The problem is that at times, the code being run in randomstuff needs to be stopped, without affecting the main program.

I have tried exit(), quit() and a few os functions, but all of them want to close my main program as well. Inside of the module, I have a thread that checks if the module should be stopped - so what function should I put in the thread when it realizes that the program must be stopped.

Any ideas on how to go about this? Thanks

user3147471
  • 63
  • 1
  • 6
  • So, how do you "run" the code in the imported module in a way that it still run while you are executing code in the main program? Threads? – Klaus D. Jan 21 '15 at 16:26
  • Yes, it is in a thread. – user3147471 Jan 21 '15 at 16:29
  • possible duplicate of [Stopping a thread after a certain amount of time](http://stackoverflow.com/questions/6524459/stopping-a-thread-after-a-certain-amount-of-time) – Klaus D. Jan 21 '15 at 16:32

2 Answers2

1

Basically I imported the module within a try except, but since I wanted to do the same thing on multiple modules, I found out a way to programmatically for loop through the modules within a single try-except and continue.

From the main program:

import importlib
importList = ['module1','module2']
for lib in importList:
    try:
        print('Importing %s' % lib)
        globals()[lib] = importlib.import_module(lib)
    except SystemExit:
        continue

From the module thread:

sys.exit()
mhega
  • 57
  • 5
0

I have a new answer as I now know what you mean. You'll have to extend the thread class with a Boolean to store the current state of the thread like so:

mythread.py

from threading import *
import time

class MyThread(Thread):
    def __init__(self):
        self.running = True
        Thread.__init__(self)

    def stop(self):
        self.running = False

    def run(self):
        while self.running:
            print 'Doing something'
            time.sleep(1.0/60)


thread = MyThread()
thread.start()

mainscript.py

from mythread import *
thread.stop() # omit this line to continue the thread
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • Sorry for the additional response, but I have just had time to test it, and it does not seem to work. I used your exact code, except added a 100ms delay in mainscript.py between importing the thread, and stopping it. – user3147471 Jan 21 '15 at 20:11
  • It works fine on my computer. The thread is initialized in one file, imported and stopped in the other. – Malik Brahimi Jan 21 '15 at 20:12
  • Try omitting `thread.stop()` and you'll see that it will continuously print. – Malik Brahimi Jan 21 '15 at 20:12
  • Yes, it works if I omit the thread.stop() as it works continuously. However, if I put in thread.stop(), it does not stop the thread. – user3147471 Jan 21 '15 at 20:15
  • It works fine for me, it's printed once then the thread stops. Try running in IDLE. – Malik Brahimi Jan 21 '15 at 20:18
  • IDLE may help you see its success. After stopping the thread the program ends and close, this might not be evident to you, but it works. – Malik Brahimi Jan 21 '15 at 20:30
  • Great, I will check it out! I really appreciate your perseverance in helping me out. Not too many people do that these days :) – user3147471 Jan 21 '15 at 21:05