title: Activating a function when file changes without the need for an infinite loop
body: <p>It's difficult to explain what I've been trying to accomplish and my shallow knowledge does not allow me to resolve this doubt, for this reason I came here to ask for help.</p>
  <p>I have an n1 program in python that performs a function if a txt file has its KB size equal to or greater than &quot;1&quot; and will not activate the function if the same file has its KB size equal to or less than &quot;0&quot;. As you may know, the way to do this is very simple. However, this n1 program only knows if this file has changed if it performs this check. And to keep it constantly up to date I do an uninterrupted check with a loop:</p>
  <pre><code>#n1 program
  
  import os
  
  def functionInative() {
      #only activated if file KB == or &gt; 1
  }
  
  while True:
      if os.stat(path-file).st_size &gt; 0:
          functionIntive() # now activate functino
  
      else:
          pass
  
  </code></pre>
  <p>When the function is executed it will do its tasks and at the end it will clean the file so that if any new information arrives in the file, <em>it will be noticed and the process will be repeated</em>. For this reason I can't use something like a &quot;break&quot; either.</p>
  <p>However, I wondered if it would be possible to let these programs somehow &quot;stand still&quot; and activate the function only when the file is filled. <strong>No need for an infinite loop checking the file until there is any modification.</strong></p>
  <p>It would be something like real-time PUSH or CHATS notifications. There is no free way a loop checking for new messages. It is simply only when they arrive that the functions are activated.</p>
  <p>I hope you have understood my big question.</p>