2

I wrote a program that reads a text file and runs an .exe for every line in the text file. This results in opening a new command line window for every time i run the .exe. The windows do close on their own once the current task is finished, but the problem is as follows:

If i have 100 lines in the text file, this means that i call the .exe file 100 times. My problem with that is if i want to cancel the run after it already started, i have to click on the red "X" to close every window one after the another.

What i am trying to do is have some sort of a command interrupt the running program and either close all upcoming windows or just stop the for loop from running.

Is it possible to write into the console a command to interrupt the current running code?

Would it be better to use some sort of a key event listener? If so, are there any built-in key listeners in Python? I can't seem to find any. Does that mean that i have to install Pygame just so i can use a key event listener?

Maybe i should try to listen to the command line and detect an exit code on one of the windows that i manually close and that way end the for loop?

7
  • What library are you using to create the windows ? Commented Jun 27, 2013 at 10:56
  • I am not creating the windows, they are created by calling the .exe file. I call it by os.system(). Every .exe that runs does some process and that process is written into the windows cmd specific window... Commented Jun 27, 2013 at 11:01
  • What is this .exe program? Can you tell us more about it? Commented Jun 27, 2013 at 11:02
  • Not really... It's something internal at my workplace. It's an .exe that receives input dir and output dir, runs on the files located in the input dir, shows the process of the run, and writes some files into the output dir. Commented Jun 27, 2013 at 11:04
  • 1
    Basically, you'll need to create threads, but with the information you gave us, it's gonna be hard to tell you what you need to do. You can create a thread that handles what you write in the console, and kill the other process Commented Jun 27, 2013 at 11:06

1 Answer 1

3

There are a few ways you could go about this. But pretty much you have one main issue - you need some sort of flag that can be switched such that the code will know it must stop. For instance, if the code is working in a while-loop, it should check at the start of this loop if the flag is valid, or if the flag is telling the loop to stop...

while flag: # do code 

There are a few ways to implement this flagging like operation for your needs. I will discuss the threading option. First, you need to understand how threading works, and then you need to mold your script such that instead of "running an executable" for each line of the text file, you would read the text file, and put all the lines into a queue, then you would have a few threads that read from that queue, and perform the desired action (like running an executable) but instead of running an external executable, you should mimick this with Python, this thread should be a daemon thread.. and it should have a main loop which checks if a flag that exists in the parent thread is turned on...

Below is an example:

from threading import Thread from Queue import Queue import sys import time class Performer(): def __init__(self): self.active = False self.queue = Queue() def action(self, line): pass # your code should be here def operate(self, text_file, threads=5): with open(text_file) as f: for line in f: self.queue.put(line) self.active = True thread_pool = [] for i in range(threads): t = Thread(target=self.__thread, name=('worker-%d' % i)) t.daemon = True t.start() thread_pool.append(t) while self.active: try: if self.queue.empty(): break except KeyboardInterrupt: self.active = False sys.exit('user + keyboard = byebye') else: time.sleep(1) def __thread(self): while self.active: if not self.queue.empty(): try: self.action(self.queue.get()) except Exception: pass # do something here 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.