There is a script that represents a bot for Telegram. Two functions that perform different tasks.

The first one responds to user requests (instantly), the other notifies users when news appears on the site, periodically requesting the page content (also instantly), but it has a timeout (do not poll the site every second, right?), So they cannot be executed sequentially.

In addition to using the threading module and running one of the functions in daemon mode, I did not find another solution. Is it too much to use such a module just to start a simple daemon function?

import threading from time import sleep def function1(): ... sleep(timeout) def function2(): ... t = threading.Thread(target=function1) t.daemon = True t.start() function2() 

    1 answer 1

    In general, I personally do not see any special problems with threading in this case. But if you want to do everything in one thread, then it is quite simple.

    You have function2() , which responds to user requests, most likely a cycle, right? So, at the end of it, insert a check whether the specified time has not passed since the previous processing of the content of the page with news, and if it passed, make a function call to function1() . You’ll get something like this (with your function names preserved):

     import time def function1(): ... def function2(): ... latest_handling_ts = time.time() while True: # очередной ответ на запрос пользователя ... # проверка, не прошло ли время timeout с момента последнего # запроса и обработки содержимого страницы с новостями now = time.time() if now - latest_handling_ts > timeout: # заданное время прошло, запрашиваем содержимое страницы # и обрабатываем его function1() latest_handling_ts = now function2() 
    • Looks good, but bad. Imagine that the site that gives the news, a little lay down. Well, quite a bit. And the pages are given for a few minutes. Users will complain about a stupid bot that does not respond "instantly." - KoVadim
    • The formulation of the problem is indicated for instant processing. But even if this is not the case, everything easily changes for this case while preserving the common logic and structure. It is necessary to use timeout for sockets, and if it is impossible to get the contents, do not update the time of the last processing. - Vadim Shender
    • one
      In general, of course, it is more correct to solve such a problem with the help of input-output multiplexing (for python there is a link to libev, for example; but here it is quite possible to get by with ordinary select ), or use asyncio in general, but it will definitely not be easier to use threading . - Vadim Shender
    • here multiplexing is good. And I hope for an instant response of sites I would not. - KoVadim
    • one
      @MaksimMelnikov, using threading is quite a normal practice. I would most likely even recommend it and leave it to you as the simplest possible solution, since in order to make it reliable and well in one thread, like the scheme I proposed, you will have to take into account a number of nuances, as stated above in the comments. - Vadim Shender