I'm developing a personal website in Flask and I'm noticing something odd about the way the local server has been working for me.
When I do the following:
$ export FLASK_APP=run.py
$ flask run
This message is displayed:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 262-302-085
That's all well and good. But when I actually go to 127.0.0.1:5000, the server hangs and nothing ever happens. When I say 'hangs', I mean that the page stays blank but the loading icon indicates a successful connection, but the content of the site doesn't load. However, when I CTRL+C to close the script, then the website loads. Before doing so, it says the following:
^C * Serving Flask app "run"
Traceback (most recent call last):
File "/usr/local/bin/flask", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/flask/cli.py", line 507, in main
cli.main(args=args, prog_name=name)
File "/usr/local/lib/python2.7/dist-packages/flask/cli.py", line 374, in main
return AppGroup.main(self, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
File "/usr/local/lib/python2.7/dist-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask/cli.py", line 432, in run_command
use_debugger=debugger, threaded=with_threads)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 708, in run_simple
inner()
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 670, in inner
fd=fd)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 564, in make_server
passthrough_errors, ssl_context, fd=fd)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 474, in __init__
socket.SOCK_STREAM)
socket.error: [Errno 9] Bad file descriptor
I thought this was an error or something, so I CTRL+C and then I get back to my command line. I refresh the page, expecting the website to go down, and it's still there! Not only that, but the terminal has seemingly resurrected the process and is displaying HTTP 2xx status codes in the terminal as I move around the site and refresh pages. Strange. So I killed both chromium and my terminal and the website STILL loaded! So I deleted my cache for the last hour, restarted my laptop, and it was STILL up! It kind of quietly failed after another ten minutes, but I really don't understand what's happening here.
Can anyone show this confused lad some mercy? Because it doesn't seem like I should have to kill the script before the website loads. Something feels off, aside from the obvious. Anyone know what I'm doing wrong? I've been able to reproduce the problem and this has happened more than once now.
Thank you.
EDIT: Fixed a sentence and clarified that the problem is reproducible.
Related
I try to create a simple flask app:
from flask import Flask
app = Flask(__name__)
if __name__ == '__main__':
app.run()
but when I add the debug:
FLASK_APP = run.py
FLASK_ENV = development
FLASK_DEBUG = 1
I got the following error:
ValueError: signal only works in main thread
here the full stacktrace
FLASK_APP = run.py
FLASK_ENV = development
FLASK_DEBUG = 1
In folder c:/MyProjectPath/api
c:\MyProjectPath\api\venv\Scripts\python.exe -m flask run
* Serving Flask-SocketIO app "run.py"
* Forcing debug mode on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 283-122-745
Exception in thread Thread-1:
Traceback (most recent call last):
File "c:\appdata\local\programs\python\python37\Lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "c:\appdata\local\programs\python\python37\Lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "c:\MyProjectPath\api\venv\lib\site-packages\flask_socketio\cli.py", line 59, in run_server
return run_command()
File "c:\MyProjectPath\api\venv\lib\site-packages\click\core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "c:\MyProjectPath\api\venv\lib\site-packages\click\core.py", line 717, in main
rv = self.invoke(ctx)
File "c:\MyProjectPath\api\venv\lib\site-packages\click\core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\MyProjectPath\api\venv\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "c:\MyProjectPath\api\venv\lib\site-packages\click\decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "c:\MyProjectPath\api\venv\lib\site-packages\click\core.py", line 555, in invoke
return callback(*args, **kwargs)
File "c:\MyProjectPath\api\venv\lib\site-packages\flask\cli.py", line 771, in run_command
threaded=with_threads, ssl_context=cert)
File "c:\MyProjectPath\api\venv\lib\site-packages\werkzeug\serving.py", line 812, in run_simple
reloader_type)
File "c:\MyProjectPath\api\venv\lib\site-packages\werkzeug\_reloader.py", line 267, in run_with_reloader
signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
File "c:\appdata\local\programs\python\python37\Lib\signal.py", line 47, in signal
handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread
The problem you are facing has to do with a bug in the Flask-SocketIO package which replaces the flask run command. Due to this Flask-SocketIO is always used even if you don’t import it. There are several solutions:
Uninstall Flask-SocketIO
Do not use flask run but run the main file of your program
Disable debugging
Disable auto loading if debugging required flask run --no-reload
Reference to the Flask-SocketIO bug: issue 817
I solved the problem thanks to #AkshayKumar007 answer on github. That was the most convenient solution for me.
Hey guys, I was also facing the same problem. So to summarize, if
you're using socket-io, don't do flask run. First, add
if __name__ == "__main__":
socketio.run(app)
At the end of your application. To run it just do
python3 __init__.py
Hope it helped.
I'm building a basic site with the dev version of Google App Engine(ver 1.9.14.1225) and I can't figure out how to enter the debugger to determine why my template variables are not rendered.
The App Engine documentation says to use: import pdb; pdb.set_trace();
https://cloud.google.com/appengine/docs/python/tools/devserver#Python_Debugging_with_PDB
However, when I inserted pdb into my code, it threw this error:
if self.quitting: raise BdbQuit
How do I enter the pdb debugger?
ERROR 2014-10-30 14:25:16,768 webapp2.py:1552]
Traceback (most recent call last):
File "/Users/Bryan/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/Users/Bryan/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/Users/Bryan/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Users/Bryan/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/Users/Bryan/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/Bryan/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/Bryan/work/GoogleAppEngine/dermalfillersecrets/main.py", line 94, in get
self.response.write(template.render(template_values))
File "/Users/Bryan/work/GoogleAppEngine/dermalfillersecrets/main.py", line 94, in get
self.response.write(template.render(template_values))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/bdb.py", line 49, in trace_dispatch
return self.dispatch_line(frame)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/bdb.py", line 68, in dispatch_line
if self.quitting: raise BdbQuit
osX 10.9.5
It looks like there is a known issue with PDB and Mac. I would "star" it to add more weight and comment that you are still seeing the issue.
Looks like it's caused because PDB uses stdin/stdout for i/o and the MAC dev_server doesn't work with them.
Looks like there's a third party tool that should work.
Here it looks like you can re-direct the I/O, I don't have a mac to test where you can re-direct but might be helpful.
There is also this tool to redirect the I/O to a socket. Not sure it will work but thought I would include it just in case.
If you are using the GUI to run your dev server I don't believe you will have access to pdb.
You should try the command line run server for app engine via:
dev_appserver.py myapp
and possibly even the django-server: django-admin runserver
08:38 AM Scanning files on local disk.
2013-01-30 08:38:30,884 ERROR appcfg.py:1856 Invalid character in filename: Icon
Error 409: --- begin server output ---
Another transaction by user SLim is already in progress for app: s~pekkylab, version: 1. That user can undo the transaction with "appcfg rollback".
--- end server output ---
Password for myemail#email.com: If deploy fails you might need to 'rollback' manually.
The "Make Symlinks..." menu option can help with command-line work.
*** appcfg.py has finished with exit code 1 ***
While i'm trying to upload i got this error and then follow its instructor to perform rollback then i getting the other error here
08:41 AM Application: pekkylab
08:41 AM Host: appengine.google.com
08:41 AM Rolling back the update.
Traceback (most recent call last):
File "/usr/local/bin/appcfg.py", line 171, in <module>
run_file(__file__, globals())
File "/usr/local/bin/appcfg.py", line 167, in run_file
execfile(script_path, globals_)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/appcfg.py", line 4377, in <module>
main(sys.argv)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/appcfg.py", line 4368, in main
result = AppCfgApp(argv).Run()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/appcfg.py", line 2605, in Run
self.action(self)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/appcfg.py", line 4103, in __call__
return method()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/appcfg.py", line 3553, in Rollback
self._Rollback()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/appcfg.py", line 3569, in _Rollback
appversion.Rollback()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/appcfg.py", line 2151, in Rollback
self.Send('/api/appversion/rollback')
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/appcfg.py", line 1842, in Send
return self.rpcserver.Send(url, payload=payload, **self.params)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/appengine_rpc.py", line 391, in Send
f = self.opener.open(req)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 391, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1181, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/fancy_urllib/fancy_urllib/__init__.py", line 379, in do_open
url_error.reason.args[1])
fancy_urllib.InvalidCertificateException: Host appengine.google.com returned an invalid certificate (_ssl.c:499: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed):
To learn more, see http://code.google.com/appengine/kb/general.html#rpcssl
Any clue why is this happening?
I'm guessing you are working on a MacOS and this file is the Icon? (read more on SU answer). You can easily verify it if you list all the files from your terminal: ls.
In order to skip that file add this in end of your app.yaml file and redeploy:
skip_files:
- ^(.*/)?Icon\r
You can read more on skipping files in the docs.
Also you might need to rollback before redeploying:
appcfg.py rollback /path/to/project
And if nothing works out for you, just try to deploy to a new application and try again another day :)
Probably there is some exotic character in the filename of an icon, as the log states.
Rename it, and update your application to use the renamed version.
You should rollback the transaction manually ("appcfg rollback"), and then try to deploy again.
Try to deploy with the parameter no_cookies in that fashion appcfg.py update . --no_cookies
You probably have a custom icon for your app folder... That generate a "hidden" Icon file, delete it.
I have created a backend for my google app that looks like this:
backends:
- name: dbops
options: dynamic
and I've created an admin handler for it:
- url: /backend/.*
script: backend.app
login: admin
Now I understand that admin jobs should be able to run forever and I'm launching this job with a TaskQueue, but for some reason mine is not. My job is simply creating a summary table in datastore from a much larger table. This table holds about 12000 records and it takes several minutes for it to process the job on the development server, but it works fine. When I push the code out to appspot and try to get it to run the same job, I'm getting what looks like datastore timeouts.
Traceback (most recent call last):
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~myzencoder/dbops.362541511260492787/backend.py", line 626, in get
for asset in assets:
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2314, in next
return self.__model_class.from_entity(self.__iterator.next())
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_query.py", line 2816, in next
next_batch = self.__batcher.next()
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_query.py", line 2678, in next
return self.next_batch(self.AT_LEAST_ONE)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_query.py", line 2715, in next_batch
batch = self.__next_batch.get_result()
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 604, in get_result
return self.__get_result_hook(self)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_query.py", line 2452, in __query_result_hook
self._batch_shared.conn.check_rpc_success(rpc)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 1224, in check_rpc_success
raise _ToDatastoreError(err)
Timeout: The datastore operation timed out, or the data was temporarily unavailable.
Anyone got any suggestions on how to make this work?
While the backend request can run for a long time, a query can only run for 60 sec. You'll have to loop over your query results with cursors.
Mapreduce will get you a result quicker by doing the queries in parallel.
In production you use the HR datastore and you can run into contention problems. See this article.
https://developers.google.com/appengine/articles/scaling/contention?hl=nl
And have a look at mapreduce for creating a report. Maybe this is a better solution.
I'm trying to run a django project locally on a brand new Mac. It was working up until yesterday, and I have no clue what happened, but all of a sudden I started getting Redis errors.
I can load pages that don't query the database, but once I try to do a search, which relies partly on Redis, it doesn't work.
Any ideas? here is the traceback.
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Library/Python/2.7/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__
return self.application(environ, start_response)
File "/Library/Python/2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
response = self.get_response(request)
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 179, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/Users/dlitwak/mozio/dotcloud/demo/search/views.py", line 391, in results
cache.setDistanceAndDuration(distance, time, request.user.username)
File "/Users/dlitwak/mozio/dotcloud/demo/cache.py", line 305, in setDistanceAndDuration
self.cache.set(key, value, 1800)
File "/Library/Python/2.7/site-packages/redis_cache/cache.py", line 218, in set
result = self._set(key, pickle.dumps(value), int(timeout), client, _add_only)
File "/Library/Python/2.7/site-packages/redis_cache/cache.py", line 199, in _set
return client.setex(key, value, timeout)
File "/Library/Python/2.7/site-packages/redis/client.py", line 1221, in setex
return self.execute_command('SETEX', name, time, value)
File "/Library/Python/2.7/site-packages/redis/client.py", line 338, in execute_command
connection.send_command(*args)
File "/Library/Python/2.7/site-packages/redis/connection.py", line 287, in send_command
self.send_packed_command(self.pack_command(*args))
File "/Library/Python/2.7/site-packages/redis/connection.py", line 269, in send_packed_command
self.connect()
File "/Library/Python/2.7/site-packages/redis/connection.py", line 217, in connect
raise ConnectionError(self._error_message(e))
ConnectionError: Error 2 connecting to unix socket: 127.0.0.1. No such file or directory.
We are running MYSQL. I can access the db through the terminal, so I don't think it's a DB access problem.
Sometimes this is because Django cannot connect to the database, and redis is throwing the error.
And since you can load pages without db, seems like it’s the case.
Ok, so we finally got it to work.
For some reason Redis is no longer being started automatically and is not running in the background.
I'm working on a Mac, and it previously did this automatically when I was running on Ubuntu. The solution was to run "redis-server" in a separate terminal window.
We are still trying to figure out how to make it start automatically, or why it stopped in the first place, but yeah.
If your using a Mac, using the Homebrew package manager is probably the best way to install and manage Redis. Once Homebrew is installed simply, brew install redis on the command line.
After it's installed you can set it up to run automatically on startup:
ln -sfv /usr/local/opts/redis/*.plist ~/Library/LaunchAgents
Then to run it now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist