Good day. I myself have been doing programming recently and am only at the initial stage. And from time to time, questions come to me that I would like to know the answers to, but apart from you, I have nowhere to turn.

Can you please tell me how to make the application run all the time?

Background: I have an application that keeps track of updates in the group in contact, as soon as a new entry appears - it is loaded into the database. I run the application, it goes over the last 10 entries and loads those that are not in the database, and so on each time. How can I make the application work all the time and when adding a record in the group it was immediately pulled into the database.

  • one
    aside: the questions on Stack Overflow must allow for a short (several paragraphs) answer, that is, if a good answer to your question requires a much larger amount, then the question should be narrowed down or you need another resource. Answers consisting only of a link to an external resource (book, articles) are not very useful: many links stop working after some time and it’s better if the web search engine in such cases is directly people, and not via Stack Overflow (i.e., questions to which you can only reply with a link, it is better to delete, so that Google does not embarrass). - jfs

3 answers 3

such code:

while True: <...> 

will work constantly until the program is forcibly completed. instead of <...> of course, you need to write the program itself.

    There are two parts here:

    1. The application does not exit by completing one task. For example, the http-server continues to work after the completion of the processing of the http-request
    2. The application is monitored by another part of the system, and if the application crashes / stops responding to the selected request, then recovery actions are performed. For example, systemd / supervisord can restart a server if its process is dead.

    Such applications are often installed as a service in the system (daemon). For example, this could be an http server that accepts a callback notifying you that a new entry has been created.

    As a rule, in your code you will not see a global while True loop, since it does not make sense to implement the event loop in each application from scratch. Here is a sample code for the http server — the event loop runs in a run() method that does not return until the server is stopped.

      It is better to do so in order not to depend on the time and order of the script launch, since the constantly running process is an additional complexity.

      Just before inserting into the database, check that there are no records with such a header yet. If there is, then all the news is already loaded on and you can finish.

      It turns out that the script can be run at any time and as many times as necessary, and the database will always contain the correct data.

      To run scripts on a schedule, they usually use cron, for example, you can run the update_news.py script every 10 minutes:

       */10 * * * * /usr/local/bin/update_news.py 
      • the author has the requirement that the changes be immediately displayed: "when adding an entry in the group, it was immediately pulled up in the database." The cron option requires constant launches (regardless of whether there are new entries / not) and the information may lag (depending on the launch interval). To avoid delays and unnecessary network requests, you can configure the webhook, which is called when new records are added, or another method that delivers the information immediately. Compare the script that cron directory reads in search of a new file, with the daemon that inotify uses; or as websockets on SO - jfs
      • "Good afternoon. I myself am recently engaged in programming and am only at the initial stage." Judging by this, the demon is too complicated. And indeed, in a similar task, the delay per minute should be acceptable, making a garden of demons completely useless. - kubus
      • For a completely beginner, it’s enough to start a script with the server in the console with hands, which is even easier than writing a script in cron (according to statistics, quite beginners probably sit on Windows) ¶ In general, the answers to SO are not just for the author, so in any case, cron can to be only a supplement, but not a basic answer on the topic "The Permanent Work of a Python Application" - jfs