Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

profile for api running in subthread #83

Open
zono opened this issue May 15, 2018 · 0 comments
Open

profile for api running in subthread #83

zono opened this issue May 15, 2018 · 0 comments

Comments

@zono
Copy link

zono commented May 15, 2018

Hi. Can I use flask-profiler for api running in subthread? I tried it like the following. However I got ValueError: signal only works in main thread error. Can you please help me to solve it?

  • files
├── server.py
└── api.py
  • server.py
import time
import api

api_server = api.APIServer()
api_server.daemon = True
api_server.start()

while True:
  time.sleep(1)
  • api.py
import threading
from flask import Flask
import flask_profiler

class APIServer(threading.Thread):

  def __init__(self):
    threading.Thread.__init__(self)

  def run(self):
    app = Flask(__name__)
    app.config["DEBUG"] = True

    app.config["flask_profiler"] = {
      "enabled": app.config["DEBUG"],
      "storage": {
        "engine": "sqlite"
      },
      "basicAuth":{
        "enabled": True,
        "username": "admin",
        "password": "admin"
      },
        "ignore": [
          "^/static/.*"
        ]
      }

    @app.route('/products', methods=['GET'])
    def listProducts():
      return "suppose I send you product list..."

    flask_profiler.init_app(app)

    app.run(host="127.0.0.1", port=5000, threaded=True)
  • command
$ python3 server.py
 * Serving Flask app "api" (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://127.0.0.1:5000/ (Press CTRL+C to quit)
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/home/xxx/flask-profiler/api.py", line 35, in run
    app.run(host="127.0.0.1", port=5000, threaded=True)
  File "/home/xxx/.local/lib/python3.5/site-packages/flask/app.py", line 943, in run
    run_simple(host, port, self, **options)
  File "/home/xxx/.local/lib/python3.5/site-packages/werkzeug/serving.py", line 812, in run_simple
    reloader_type)
  File "/home/xxx/.local/lib/python3.5/site-packages/werkzeug/_reloader.py", line 267, in run_with_reloader
    signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
  File "/usr/lib/python3.5/signal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant