0

I'm using pyqt as an infinity loop, but I don't know how to escape from it programmatically, or pythonically. below is my code.

from PyQt4.QtGui import QApplication

loop = QApplication([])  
main()                   
loop.exec_()

I want to write in my main() function like, if some condition is satisfied, then escape.

I'm absolutely new to programming, I've been trying to find any clue on google, like close() or something, nothing works.

any help, hint would be appreciated. thank you.

user159234
  • 171
  • 1
  • 1
  • 5
  • Judged by the comments below, what you're trying to do is a pretty good example for a "producer consumer pattern", where one thread produces something (like getting prices from a market) and another thread consumes this information - your trading algorithm. Googl'ing for it you can find e.g. this example: http://agiliq.com/blog/2013/10/producer-consumer-problem-in-python/ – sebastian Aug 10 '15 at 10:49

1 Answers1

1

Before I give you my solution, can I ask why you are intentionally using an infinite loop? An infinite loop is exactly what it states, meaning it continues infinitely. Unless you have some conditional check in your loop that can check if some number or value is hit, then closes out, your loop will continue indefinitely.

Now for a solution: Pressing Ctrl-C in your terminal (or where-ever you're running this loop) will stop the program running. This is a universal command as well. Program-wise, using break will break your loop. I hope this answers your question.

Here is a code snippet that might help with what you're doing:

def main():
    while(Some Boolean Value):
        #Things you want to do in your loop
        if(Some Boolean Value):
            break

The best course of action for your issue would be multi-threading. Here are two links that address what you're wanting to do:

Stopping a thread after a certain amount of time

Is there any way to kill a Thread in Python?

Community
  • 1
  • 1
Jason Cromer
  • 1,468
  • 3
  • 11
  • 21
  • because I'm making auto-trading algorithm. my program need to stay while the market is open, and whenever it gets price value from the stock firm API, it have to process the algorithm. the part that waits to get the values forced me to use an infinity loop, so I first used while 1:, it takes to much resources, and I turn to pyqt. – user159234 Aug 05 '15 at 16:36
  • and about your solution, I know ctrl+C, what I want is some automatic solution like, in my code, I want to put the escaping part in. – user159234 Aug 05 '15 at 16:38
  • Ok, so what you can do is make a function that continuously re-assigns a stock market value. For instance, in your loop, grab the stock price value, assign it to a variable (This is where classes come in handy, so you can assign it to "this.stockPrice"), then check the price of "this.stockPrice" in your while loop. This way, whenever the stock price changes, your while loop breaks. – Jason Cromer Aug 05 '15 at 16:40
  • anyways, thank you for your answer. is there any method that my code operate to press ctrl+C on the code? if it is possible, would be great – user159234 Aug 05 '15 at 16:41
  • no, I can't use while loop. like I said it took too much resources. – user159234 Aug 05 '15 at 16:42
  • I used to take that method, but it didn't work out well. now the code works first make the infinity loop, wait, get the value, run the algorithm, and take action, and wait for the next value. the problem is when the market is over, it still running. so i have to terminate it like ctrl+C or close the window. what i want to do is automate the part. – user159234 Aug 05 '15 at 16:46
  • What do you mean too much resources? – Jason Cromer Aug 05 '15 at 16:47
  • Ahhh ok. Is there a function in the library's API or Documentation that allows you to check whether or not the market is closed? Also, another work-around is to use the python module "datetime" to get your current time, and end the program when the stock market ends at your local timezone. https://docs.python.org/2/library/datetime.html – Jason Cromer Aug 05 '15 at 16:50
  • like, my cpu is burning. during the waiting part, which is the biggest part, the while loop runs constantly. – user159234 Aug 05 '15 at 16:50
  • no, I know when the market is closed, like you said importing time. but I CAN'T stop my loop. that's the problem. break doesn't work, close() doesn't work, I used everything i know. – user159234 Aug 05 '15 at 16:52
  • Also, consider using range() instead of a while loop. Since Python is written over C, range() is implemented in C and runs much quicker than a while loop, thus using much, much less resources. Here is a link that goes into more detail: http://stackoverflow.com/questions/869229/why-is-looping-over-range-in-python-faster-than-using-a-while-loop – Jason Cromer Aug 05 '15 at 16:52
  • 1
    Ok, lastly, I could recommend using multi-threading. You can have one thread running the loop, while the other one checks for the time and closes the first thread when the market closes. This will solve your problem, however, you will have to get the hang of multi-threading. :) – Jason Cromer Aug 05 '15 at 16:54
  • ctrl-break is generally better better than ctrl-c – ArduinoBen Apr 18 '22 at 09:01