2020年12月21日星期一

__init__ method runs multiple times

Here is my (simplified) flask code

from flask import Flask, request, redirect  from api_v1.api import api as api_blueprint    app = Flask(__name__)  app.register_blueprint(api_blueprint, url_prefix='/api/v1')  

here is api.py

from flask import Blueprint, request  from api_v1.db.connect_db import Dbs      db = Dbs() api = Blueprint('api/v1/', __name__)  

here is connect_db.py

import mysql.connector      class Dbs:      def __init__(self):          print("connecting")          self.source_db = mysql.connector.connect(              # credentials goes here          )          self.main_db = mysql.connector.connect(             # credentials goes here          )          self.source_cursor = self.source_db.cursor()          self.main_cursor = self.main_db.cursor()          print("connected")  

output:

connecting

connected

  • 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
  • Restarting with stat

connecting

connected

  • Debugger is active!
  • Debugger PIN: 000-00-000 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)

The problem is, THIS CONNECTS TWICE TO THE DATABASES

And it's showing duplicate connections from the app in my mysql server

  1. Why is this happening?
  2. Anyone know a solution ?
https://stackoverflow.com/questions/65402756/init-method-runs-multiple-times December 22, 2020 at 11:07AM

没有评论:

发表评论