I am running a simple Flask application on Ubuntu 14 with (I believe) Python 2.7 (having difficulties with 3.x in the virtualenv). Every time I try to close my application with CTRL+C, it launches the app again immediately. If I close it again, it exits, but leaves one process running I can see with ps -a.
If I then run the .py file again, it starts up, then if I exit with CTRL+C, it throws socket.error: [Errno 98] Address already in use and exits.
At this point there are two processes visible with ps -a. Any subsequent startups immediately throw the socket.error.
I have noticed this error is only present when the line app.run(host='0.0.0.0') is left in (changing the IP has no effect).
I am unsure why, and looking for any help into the matter.
Shell
Code
Use
app.run(host=‘0.0.0.0’, debug=True)
instead of
app.run(debug=True)
app.run(host=‘0.0.0.0’)
The latter should start 2 apps continuously.
So when you hit ctrl+C the first time, you are actually terminating the first line, but immediately after that the second line executes and another app starts.
Related
The python file in question collects data from API and the server is down once in a while so it stops running. to solve this, I've written this batch file that would keep running the file even if it encounters an error, so in few minutes it would be running as usual.
:1
python3 datacollection.py
goto 1
Now, how do I achieve the same with bash file?
Note: I want to run the python file on AWS EC2 Ubuntu.
Just to add to #Willian Pursell answer. You could run screen command on your AWS server, then run while sleep 1; do python3 datacollection.py; done in your newly created session. Finally detach to the session using Ctrl + ad keys. With that, you will be able to exit your session. Exit the server, the command will keep running on the background. Cheers.
while sleep 1; do python3 datacollection.py; done
Don't forget the sleep, or you will be annoyed when it falls into a crash loop with python3 aborting immediately.
Use some kind of process manager e.g pm2 for this. It will be more stable than that.
I start Django server with python manage.py runserver and then quit with CONTROL-C, but I can still access urls in ROOT_URLCONF, why?
Probably you left another process running somewhere else.
Here is how you can list all processes whose command contains manage.py:
ps ax | grep manage.py
Here is how you can kill them:
pkill -f manage.py
Without seeing your script, I would have to say that you have blocking calls, such as socket.recv() or os.system(executable) running at the time of the CTRL+C.
Your script is stuck after the CTRL+C because python executes the KeyboardInterrupt AFTER the the current command is completed, but before the next one. If there is a blocking function waiting for a response, such as an exit code, packet, or URL, until it times out, you're stuck unless you abort it with task manager or by closing the console.
In the case of threading, it kills all threads after it completes its current command. Again, if you have a blocking call, the thread will not exit until it receives its response.
just type exit(), that is what I did and it worked
I've seen write-ups but am not sure how to apply this to my example. I want to be able to push a button on my webpage, run a piece of python code -- and within the python code it runs another process (executable). The executable takes time, so I want the webpage to not wait until the executable is done running.
I have seen things on daemon and forks, but am not sure how to implement them. I am running an Apache Server with Python CGI on a Windows 8 machine.
Currently, my python code runs and the executable runs fine, but the web page waits until the exe is done processing before updating with an alert window that the "Request has been submitted". Here is my popen code as it is now:
p = subprocess.Popen(['test_sendemail.exe',cmd],shell=False,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
Again, everything works fine here, but I need the python script to end even though the exe is running so that it can update the webpage and move on. (once the exe is done running, it fires an email off with results -- which is why I don't want to wait on the webpage for it to finish).
Any thoughts? Thanks!
Updated post:
I have a python web application running on a port. It is used to monitor some other processes and one of its features is to allow users to restart his own processes. The restart is done through invoking a bash script, which will proceed to restart those processes and run them in the background.
The problem is, whenever I kill off the python web application after I have used it to restart any user's processes, those processes will take take over the port used by the python web application in a round-robin fashion, so I am unable to restart the python web application due to the port being bounded. As a result, I must kill off the processes involved in the restart until nothing occupies the port the python web application uses.
Everything is ok except for those processes occupying the port. That is really undesirable.
Processes that may be restarted:
redis-server
newrelic-admin run-program (which spawns another web application)
a python worker process
UPDATE (6 June 2013): I have managed to solve this problem. Look at my answer below.
Original Post:
I have a python web application running on a port. This python program has a function that calls a bash script. The bash script spawns a few background processes, then exits.
The problem is, whenever I kill the python program, the background processes spawned by the bash script will take over and occupy that same port.
Specifically the subprocesses are:
a redis server (with daemonize = true in the configuration file)
newrelic-admin run-program (spawns a web application)
a python worker process
Update 2: I've tried running these with nohup. Only the python worker process doesnt attempt to take over the port after I kill the python web application. The redis server and newrelic-admin still do.
I observed this problem when I was using subprocess.call in the python program to run the bash script. I've tried a double fork method in the python program before running the bash script, but it results in the same problem.
How can I prevent any processes spawned from the bash script from taking over the port?
Thank you.
Update: My intention is that, those processes spawned by the bash script should continue running if the python application is killed off. Currently, they do continue running after I kill off the python application. The problem is, when I kill off the python application, the processes spawned by the bash script start to take over the port in a round-robin fashion.
Update 3: Based on the output I see from 'pstree' and 'ps -axf', processes 1 and 2 (the redis server and the web app spawned by newrelic-admin run-program) are not child processes of the python web application. This makes it even weirder that they take over the port that the python web application occupies when I kill it... Anyone knows why?
Just some background on the methods I've tried to solve my above problem, before I go on to the answer proper:
subprocess.call
subprocess.Popen
execve
the double fork method along with one of the above (http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/)
By the way, none of the above worked for me. Whenever I killed off the web application that executes the bash script (which in turns spawns some background processes we shall denote as Q now), the processes in Q will in a round-robin fashion, take over the port occupied by the web application, so I had to kill them one by one before I could restart my web application.
After many days of living with this problem and moving on to other parts of my project, I thought of some random Stack Overflow posts and other articles on the Internet and from my own experience, recalled my experience of ssh'ing into a remote and starting a detached screen session, then logging out, and logging in again some time later to discover the screen session still alive.
So I thought, hey, what the heck, nothing works so far, so I might as well try using screen to see if it can solve my problem. And to my great surprise and joy it does! So I am posting this solution hopefully to help those who are facing the same issue.
In the bash script, I simply started the processes using a named screen process. For instance, for the redis application, I might start it like this:
screen -dmS redisScreenName redis-server redis.conf
So those processes will keep running on those detached screen sessions they were started with. In this case, I did not daemonize the redis process.
To kill the screen process, I used:
screen -S redisScreenName -X quit
However, this does not kill the redis-server. So I had to kill it separately.
Now, in the python web application, I can just use subprocess.call to execute the bash script, which will spawn detached screen sessions (using 'screen -dmS') which run the processes I want to spawn. And when I kill off the python web application, none of the spawned processes take over its port. Everything works smoothly.
I use IDEA 10.5 for my Flask experimentation. Flask has en embedded test server (like Django does)
When I launch my test class, the dev server launches as well on port 5000. All good.
* Running on http://127.0.0.1:5000/
When I click on the "Stop process" button (red square), I get the message saying the process is finished :
Process finished with exit code 143
However the server is still alive (responds to requests) and I can see I still have a python process running.
Obviously this prevents me from relaunching the test straight away, I have to kill the server process first.
How do you manage to get both your program and the server ending at the same time ?
I guess what happens is that you start your flask app which then is forking the development server as a new process. If you stop the app the forked process is still running.
This looks like a problem, that cannot easily be solved within the means of your IDE. You could add something to your main to kill the already running server process, before starting the app again, but that seems ugly.
But why don't you just start your app with app.run(debug=True) as described in flask doc? The server will reload automatically everytime you changed your app so you don't have to stop and restart it manually.
EDIT:
Something a bit quirky just came to my mind: if you just need a comfortable way to kill the server from within the IDE all you have to do is to introduce a syntactical error in one of the places the reloader monitors, save the file and the server will choke on it and die :)
This doesn't happen anymore with newer versions (tested with PyCharm 2.0)