I was running flask in a public_ip server(with some users)
run command is
(host='0.0.0.0', port=80, debug=True)
but flask server blocking somewhere one hour ago (the log show last request is one hour before)
so how can I debug on it?(figure out it's blocking on which python line?)
I have tried
gdb python3.4-dbg pid,but my flask app can't run by python3.4-dbg,for
from PIL import _imaging as core
ImportError: cannot import name '_imaging'
I believe the command is:
gdb -p pid
to attach to a running process.
oh,I find a way
after install python-dbg
use gdb python pid to attach flask
and use py-bt py-list py-locals to check the blocking stack
Related
Hope you're well!
I have a problem with the Psycopg module allowing to interact with the PostgreSQL database.
When I start my HTTP server (I use FastAPI and Uvicorn) normally, and I send a request to my server, I have this error:
error connecting in 'pool-1': Psycopg cannot use the 'ProactorEventLoop' to run in async mode. Please use a compatible event loop, for instance by setting 'asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())'
I have already done what the error solution suggests, that is, set the event loop policy to WindowsSelectorEventLoopPolicy, which I did.
import asyncio
from asyncio import WindowsSelectorEventLoopPolicy
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
I put this line in my main file (which is the central file of my app)
But still nothing, I still have the same error when I send a request to my server.
But what is strange is that when I start my server with the --reload option (which is used to reload the files automatically in case of modification but is not adapted for production, according to the documentation of Uvicorn/FastAPI), I have no error when I send my requests and everything goes correctly
Can you tell me what is the cause of the problem and how to solve it?
P.S: Here is how i start my HTTP from Windows Powershell
Here is how I start my HTTP server normally (which doesn't works)
uvicorn src.main:app --port 2314
Here is how I start my HTTP server with the --reload option (which works perfectly)
uvicorn src.main:app --port 2314 --reload
Uvicorn sets the WindowsSelectorEventLoopPolicy policy when using the reload option and Windows by default, otherwise it doesn't. That's why you are facing this mismatch behavior.
Changing another policy should have worked, maybe it's misplaced.
The below works:
import asyncio
import uvicorn
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
async def app(scope, receive, send):
print(asyncio.get_event_loop_policy())
if __name__ == "__main__":
uvicorn.run("main:app", port=8010)
If we remove the line on which we set the policy, and we have uvloop installed (uvicorn automatically selects uvloop if installed), we can see that the uvloop policy is printed.
In case it's not misplaced, I recommend open a discussion on uvicorn.
Disclaimer: I'm a maintainer of Uvicorn.
Thank you! After having made several tests and having read again the documentation of uvicorn.
Everything was a little bit my fault
I launched the uvicorn HTTP server (without the --reload option) directly from the command line from a terminal (in my case Powershell), I got the error.
error connecting in 'pool-1': Psycopg cannot use the 'ProactorEventLoop' to run in async mode. Please use a compatible event loop, for instance by setting 'asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())'
Because this way of doing things didn't read the import I had done in my main.py file, but simply started the server without caring about anything, hence the error asking me to set the event loop policy to WindowsSelectorEventLoopPolicy (which I had done), simply because the import line defining my event loop policy was never reached.
So now instead of launching my uvicorn server directly in the terminal like this
uvicorn src.main:app --port 2314
I execute my python code like this
python -m src.main
and I had to modify my code in main.py like this
import asyncio
from asyncio import WindowsSelectorEventLoopPolicy
import uvicorn
from fastapi import FastAPI
...
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
app = FastAPI()
...
if __name__ == '__main__':
uvicorn.run('src.main:app', host="127.0.0.1", port=2314)
The reason why it was working without any problem in the terminal is by running my HTTP server with the --reload option like this
uvicorn src.main:app --port 2314 --reload
is that as #marcelo-trylesinski said, the --reload option automatically sets event loop to WindowsSelectorEventLoopPolicy. I wish this could be notified / mentioned in the documentation.
Thanks again to #marcelo-trylesinski for his help
I'm building a Flask web app and would like to run some background services when the app starts up.
For example, in the web app you have the ability to add service accounts, routers ip's, server ip's and such. I'd like to have some background services running network scans, wmi calls and some other tasks to constantly update the database with relevant information.
In Rails I've used initializers in config/initializers to start some daemons:
# Start all the daemons to update the DB
system('script/daemon restart vs_updater.rb')
system('script/daemon restart c_fw_updater.rb')
system('script/daemon restart sans_updater.rb')
system('script/daemon restart sf_updater.rb')
Is there such an equivalent for Flask? Or should I just build separate scripts and run them in a different manner?
you can add commands to the __init__.py file in the root directory and use subprocess to run the script
from subprocess import call
call("script/daemon restart vs_updater.py")
fab
http://www.fabfile.org/
subprocess
https://docs.python.org/2/library/subprocess.html
I followed the tutorial here: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04 and created a Flask app hosted on an Ubuntu server on Digital Ocean. To update the website, I need to ssh into my cloud server and call "sudo reload myproject" from the command line. I would like to automate this and have the python code in Flask call this command every time it gets a post request.
I tried using os.system('sudo reload myproject') but that does not work. How would I use Flask to reload the project?
in /etc/sudoers.d/myOverrides.tmp, I have the following line:
user ALL=NOPASSWD: ALL
the code in my flask module:
from flask import Flask, render_template, request
import os
#application.route("/", methods=['GET', 'POST'])
def hello():
os.system('sudo reload halo')
if (request.method == 'POST'):
print 'post request'
os.system('sudo reload halo')
return "<h1>reloaded</h1>"
else:
return "<h1>default</h1>"
When I type in "sudo reload halo" on the command line, it reloads without a problem. I would like the Flask application to execute the same command.
This seems like a bad idea to me. Providing your web app sudo access and making that call publicly available screams liability.
Instead here are a couple of other options:
Automate it with a ssh command
You can actually run a command over SSH without having to do a full login yourself. You can continue to use the user you are using at the moment, but set up passwordless login to use a public-private key pair.
Then call ssh -i /path/to/keyfile username#server 'sudo reload halo'
Auto reload with gunicorn
gunicorn has a --reload option builtin you should consider using as well.
I would like to debug an uWsgi application in Eclipse. Is there a way to create a run profile in Eclipse so that it can attach the debugger to the uWsgi python interpreters?
Found some way by using the pydev remote debugger:
in the code you want to debug import pydevd
at the point you want to break call settrace
start your uwsgi container
start the pydev remote debugger (see link)
make a http call executing your code in uwsgi
et voile. Breaking.
code:
import sys
sys.path.append("/path/to/eclipse indigo/plugins/org.python.pydev_2.7.0.2013032300/pysrc/")
import pydevd
def application(env, start_response):
#break here
pydevd.settrace()
I am porting a windows application to OS X 10.6.8. It is a new platform for me and I am facing some difficulties.
The application is a small webserver (bottle+waitress) which is starting a browser (based on chromium embedded framework) thanks to a subprocess call.
The browser is an app file and runs ok when started from gui.
I am launching it this way:
subprocess.Popen([os.getcwd()+"/cef/cefclient.app", '--url=http://127.0.0.1:8100'])
Unfortunately, this fails with OSError: permission denied.
I tried to run the script with a sudo with similar result.
I can launch the app from shell with the following command:
open -a "cef/cefclient.app" --args --url-http://127.0.0.1:8100
But
subprocess.Popen(['open', '-a', os.getcwd()+'/cef/cefclient.app', '--args', '--url-http://127.0.0.1:8100'])
fails with the following error
FSPathMakeRef(/Users/.../cefclient.app) failed with error -43.
Any idea how to fix this issue?
The file cefclient.app is actually a directory (an application bundle, specifically), not the application executable. The real executable is located inside the bundle, with a path like Contents/MacOS/executable_name. So to launch it, you'd do this:
subprocess.Popen([os.getcwd()+"/cef/cefclient.app/Content/MacOS/executable_name",
"--url=http://127.0.0.1:8100"])
Alternatively,
os.system('open -a "cef/cefclient.app" --args --url-http://127.0.0.1:8100')
Just depends if you want to control stdin / stdout or if starting the app is enough.