Start/stop flask with supervisord - python

I need to run a small flask server on Ubuntu and keep it up in case it crashes. I'm using supervisor to do that. It starts up fine the first time, but if it's restarted by supervisor (manually) the port is already occupied.
If the flask server is run from the command line it shuts down fine. The process does not seem to daemonize itself, which I understand can be an issue with supervisord.
(venv) tom#tom-desktop:~/Emotional_Mirror/emotional-mirror-backend$ python twitterEmoServer/twitterEmoServer.py
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 133-851-307
^C(venv) tom#tom-desktop:~/Emotional_Mirror/emotional-mirror-backend$
Error from supervisor logs:
s.bind((hostname, port))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use
Traceback (most recent call last):
File "/home/tom/Emotional_Mirror/emotional-mirror-backend/twitterEmoServer/twitterEmoServer.py", line 116, in <module>
app.run(host= '0.0.0.0', debug=True)
File "/home/tom/Emotional_Mirror/emotional-mirror-backend/twitterEmoSpider/venv/local/lib/python2.7/site-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/home/tom/Emotional_Mirror/emotional-mirror-backend/twitterEmoSpider/venv/local/lib/python2.7/site-packages/werkzeug/serving.py", line 677, in run_simple
s.bind((hostname, port))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use

Related

raise ValueError("cannot have a multithreaded and multi process server.") ValueError: cannot have a multithreaded and multi process server

Serving Flask app "server" (lazy loading)
Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
Debug mode: on
Running on http://0.0.0.0:9002/ (Press CTRL+C to quit)
Restarting with stat
Debugger is active!
Debugger PIN: 314-390-242
Exception in thread Thread-1:
Traceback (most recent call last):
File "/home/user/Downloads/yes/envs/emotionservice/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/home/user/Downloads/yes/envs/emotionservice/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/user/Downloads/yes/envs/emotionservice/lib/python2.7/site-packages/werkzeug/serving.py", line 963, in inner
fd=fd,
File "/home/user/Downloads/yes/envs/emotionservice/lib/python2.7/site-packages/werkzeug/serving.py", line 803, in make_server
raise ValueError("cannot have a multithreaded and multi process server.")
ValueError: cannot have a multithreaded and multi process server.
it solved.
set threaded=True
processes=1
If you want to use flask in multiprocessing mode (processes > 1). include threaded=False, otherwise it throws same error.
app.run(host=addr, port=port, threaded=False, processes=50)

how to make run Flask in Pythonista iOS?

I am using Pythonista app on iOS and I cant make Flask module work even with the most basic code.
my code is :
from flask import Flask
Flaskapp = Flask(__name__)
#Flaskapp.route('/')
def helloWorld():
return 'Woala'
Flaskapp.run(debug=True)
but keep receiving:
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
Traceback (most recent call last):
File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/app.py", line 9, in <module>
Flaskapp.run(debug=True)
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/flask/app.py", line 990, in run
run_simple(host, port, self, **options)
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/werkzeug/serving.py", line 1007, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/werkzeug/_reloader.py", line 332, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "/private/var/mobile/Containers/Shared/AppGroup/C4F03A05-D634-4F19-865F-6072E8D6DC38/Pythonista3/Documents/site-packages-3/werkzeug/_reloader.py", line 176, in restart_with_reloader
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
File "/var/containers/Bundle/Application/E373BC34-F149-4EF5-9D0E-5CD807AB0771/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/subprocess.py", line 268, in call
with Popen(*popenargs, **kwargs) as p:
File "/var/containers/Bundle/Application/E373BC34-F149-4EF5-9D0E-5CD807AB0771/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/subprocess.py", line 708, in __init__
restore_signals, start_new_session)
File "/var/containers/Bundle/Application/E373BC34-F149-4EF5-9D0E-5CD807AB0771/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/subprocess.py", line 1261, in _execute_child
restore_signals, start_new_session, preexec_fn)
PermissionError: [Errno 1] Operation not permitted
I have already installed flask trough pip in launch_stash.py
I received solution on another forum by user augusto, so I believe its worth sharing it here as well, as it works perfectly.
“The only way to use debug is:
if __name__ == '__main__':
app.run(use_reloader=False, debug=True)”
https://forum.omz-software.com/topic/5758/pythonista-flask-error-errno-1
The iOS sandbox has limitations that will prevent you from creating subprocesses
The flask development server is attempting to create a subprocess for detecting changes to your code that will then reload the server when you make changes. You can disable this by passing "--no-reload" flask run --no-reload. This would mean that you would have to manually restart your development server when you make changes
Change Pythonista’s default interpreter to Python 2.7.

Python flask reloader "Errno 2 No such file or directory" error when running on docker

I have a problem running a little Flask app with Docker. Everything works fine with debug off, but when I try to run it with debug on it fails at the reloader stage. It works fine on Windows, the problem only comes up with Docker.
In my docker repo I install caffe and all the dependencies I need (flask and wekrzeug). I thought the problem could be with the reloader so I also installed watchdog but it still shows up. Doesn't matter whether I run app.py itself or call python -m flask run.
docker run --volume=%cd%:/workspace -p 5001:5000 caffe:cpu
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with inotify reloader
Traceback (most recent call last):
File "app.py", line 53, in <module>
app.run(debug = True, host='0.0.0.0')
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 943, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 988, in run_simple
run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "/usr/local/lib/python2.7/dist-packages/werkzeug/_reloader.py", line 176, in restart_with_reloader
exit_code = subprocess.call(args, env=new_environ, close_fds=False)
File "/usr/lib/python2.7/subprocess.py", line 523, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I've encountered the same problem here and the solution for me was to remove the executable flag from the "run.py" file.
$ chmod -x run.py
The error was very similar, but in my case I'm using an Ubuntu 18.04 WSL on a Windows 10 machine.
Try to run the app as a module:
python -m flask run
See https://github.com/pallets/flask/issues/1829 for more information.

Flask Google Cloud App Engine: OSError: [Errno 98] Address already in use

I am trying to deploy a flask app on google cloud app engine. It runs smooth in my virtual environment locally but I get an 502 error running it in the cloud.
Now I am trying to debug my code on the cloud server, using debug mode and SSH into my instance. Using docker exec -it [ID] /bin/bash I am able to to access the root of my application. Now I upon running python app.py I get the following error:
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Traceback (most recent call last):
File "app.py", line 479, in <module>
app.run(port=8080)
File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 941, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 814, in run_simple
inner()
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 774, in inner
fd=fd)
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 660, in make_server
passthrough_errors, ssl_context, fd=fd)
File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 577, in __init__
self.address_family), handler)
File "/usr/local/lib/python3.6/socketserver.py", line 453, in __init__
self.server_bind()
File "/usr/local/lib/python3.6/http/server.py", line 136, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/local/lib/python3.6/socketserver.py", line 467, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
I've been trying to kill those processes listed when I run:
ps -fA | grep python
However, this does not solve the problem of the address being in use. Also changing the port in the app.run() does not solve the issue for me.
I had a similar problem, it was caused by the Flask app also being run when the module was loaded, because I had
if __name__ == "__main__":
app.run()
at the bottom. Note that the recent requirement to name your sever file "main.py" could cause this bug to emerge.
I think the problem is that you don't need to specialize the port for cloud. Google Cloud finds the port to run your app on its own. So instead of app.run(port=8080) just write app.run()
While I was not able to figure out how to "free" the running address, I solved the problem by starting another flask process by running it on a different port like so:
flask run --port=80

Start HTTP/HTTPS server, python -m SimpleHTTPServer

I am trying to start HTTPS server on my localhost, but getting below error. I have opened cmd as an administrator.
C:\Windows\system32>python -m SimpleHTTPServer
Traceback (most recent call last):
File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Python27\lib\SimpleHTTPServer.py", line 224, in <module>
test()
File "C:\Python27\lib\SimpleHTTPServer.py", line 220, in test
BaseHTTPServer.test(HandlerClass, ServerClass)
File "C:\Python27\lib\BaseHTTPServer.py", line 595, in test
httpd = ServerClass(server_address, HandlerClass)
File "C:\Python27\lib\SocketServer.py", line 419, in __init__
self.server_bind()
File "C:\Python27\lib\BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "C:\Python27\lib\SocketServer.py", line 430, in server_bind
self.socket.bind(self.server_address)
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10013] An attempt was made to access a socket in a way forb
idden by its access permissions
Your windows firewall may block python from listening.
Additionaly, try adding a different port number higher than 1024 to the command line and see if it helps.
I was not able to start since some service is already running on 8000,
I was able to successfully start the service on another port 2000
C:\>python -m SimpleHTTPServer 2000
Serving HTTP on 0.0.0.0 port 2000 ..
One can get the PID/status for port 8000 by netstat -ano
C:\>netstat -ano
Active Connections
Proto Local Address Foreign Address State PID
TCP 0.0.0.0:912 0.0.0.0:0 LISTENING 2544
TCP 0.0.0.0:8000 0.0.0.0:0 LISTENING 4
TCP 0.0.0.0:19781 0.0.0.0:0 LISTENING 3100
shows PID 4 which is a system process.

Categories