i run a cgi script on an apache server (xampp). The script basically runs in an infinte loop. After like 3 minutes i get the error message "script timed out before returning headers". I've searched through the internet and found stuff like:

change MAX_EXECUTION_TIME to 0 -> didn't work
set_time_limit(0) -> didn't work
socket.setdefaulttimeout(0) -> didn't work

I think the error is caused because my script never returns anything to the website, but that's just like it's intended. Basically the script should be started through a website and run until i tell it to stop (it's done with the script constantly checking for a file).
One solution i thought about was a script that restarts my script if it's terminated. But a far more elegant solution would be that the script runs without being terminated by the server.
I hope everything is explained well and somebody can help me because i'm stuck with this problem for far too long and it's starting to annoy me.
One thing you could try:

Return a proper HTML header
Print a dot . every few seconds, just to keep the connection alive
disable mod_deflate in your Apache server to prevent HTTP compression
add SetEnv no-gzip to your .htaccess file
script timed out before returning headers cgi