0

I am running a Flask webserver that uses a get request to change the colors of an led strip. However, one of the light sequences requires me to run a loop that can only be changed with another get request . Ive tried everything that I could think of but I'm at a loss.

Things I've Tried: Updating global variables with following request that are conditioned to end the loop: Causes memory leaks and does not function.

Creating a separate sub process of the loop by itself in python: I can start the process but .kill(), .terminate(), and proccess.call(["kill",proccess.pid]) cant kill it.(even with the subsequent p.wait())

multiprocessing with python: None of the functions given can kill the execution

Any Help is appreciated Thanks in advance Ps: sorry if this is a bad question relatively new to stackOverflow

  • I think `global` will not be helpful. A readymade solution is to `pickle` your `state` that is what is the current LED status and store it in local storage `pickle.dump()`. When you get request then read from local storage and then take action. https://wiki.python.org/moin/UsingPickle – Epsi95 Feb 11 '21 at 04:11
  • You can check the existence of a marker file (`/var/tmp/blink_lights`). That being said, are you running an infinite loop inside a Flask view? That doesn't sound right. – Selcuk Feb 11 '21 at 04:11
  • check https://stackoverflow.com/questions/32815451/are-global-variables-thread-safe-in-flask-how-do-i-share-data-between-requests – Epsi95 Feb 11 '21 at 04:11
  • I think you are on the right path by externalizing the state in a process particular as you mentioned the infinite loop. Have you figured out why you cannot kill the process? How do you know the pid for instance? The next step is to communicate with the external process (i.e. IPC) and you can do that with signals, file system (file or named pipe), database, shared memory, socket etc. – Allan Wind Feb 11 '21 at 04:21
  • I know the process id because when i instantiate the subprocess variable process=POpen(["sudo","python3","lights.py"],shell=False) process.pid returns the process id. I dont want to use a database because making a query every iteration of the loop with be way to slow for what i need. So i think Im going to try memcached like Epsi95 suggested that seems to be applicable for what im doing. – Symney Cameron Feb 11 '21 at 04:54

0 Answers0