Mugiwara
Mugiwara

Reputation: 1

How to make an api for a python app that is in a continuous loop?

I have a python app that calls a recursive method which runs forever. It is a loop that scrapes a webpage and looks for a number and once it finds it, it prints out a message, increments the number, and calls the method again with the incremented number. This goes on forever because the webpage updates itself about once a week and my method prints out the message when that update is caught.

I want to make a mobile app that notifies users when the method prints out a message (ideally within a minute or two of the change). What is the best way to create an api that would allow me to do this? If there is another way, how can i do it?

Upvotes: 0

Views: 366

Answers (1)

Abbas Dehghan
Abbas Dehghan

Reputation: 381

Using recursive method for infinite loop is a big mistake because every time you call method again the last method goes to stack and if you do it infinite time finally you get stack overflow error. best way for infinite jobs are thread with a simple "while True":

import threading

SenderThread = threading.Thread(target=sender)
SenderThread.start()

def sender():
    while True:
        # do your job here

edit:

according to this:

Python threads are used in cases where the execution of a task involves some waiting. One example would be interaction with a service hosted on another computer, such as a webserver. Threading allows python to execute other code while waiting; this is easily simulated with the sleep function.

The reason i used thread is for the main program to do its job or respose to inputs or any thing else that you need.

Upvotes: 1

Related Questions