title: There was a problem building a simple server with fastapi using Gunicorn (feat. worker)
body: <p>I did a simple backend test with fastapi</p>
  <p>put data in docs using post and i readed data use get</p>
  <p>However, there are times when data comes out and times when it is not updated, every time you press F5,</p>
  <p>My code is very simple, and I'll show the code</p>
  <p>What's the problem? and how do solve it?</p>
  <p>db_class.py</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>from sqlalchemy import Column, TEXT, INT, BIGINT
  from sqlalchemy.orm import declarative_base
  
  Base = declarative_base()
  
  class Test(Base):
      __tablename__ = 'test'
      id = Column(BIGINT,nullable=False, autoincrement=True, primary_key=True)
      name = Column(TEXT, nullable=False)
      number = Column(INT, nullable=False)</code></pre>
  </div>
  </div>
  </p>
  <p>db_conn.py</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>from sqlalchemy import *
  from sqlalchemy.orm import sessionmaker
  
  conn_string = "mysql+pymysql://root:password@mariadb:3306/example"
  
  class engineconn:
  
      def __init__(self):
          self.engine = create_engine(conn_string, pool_recycle =500)
  
      def sessionmaker(self):
          Session = sessionmaker(bind=self.engine)
          session = Session()
          return session
  
      def connection(self):
          conn = self.engine.connect()
          return conn</code></pre>
  </div>
  </div>
  </p>
  <p>main.py</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>from fastapi import FastAPI
  from pydantic import BaseModel
  from db_conn import engineconn
  from db_class import Test
  
  app = FastAPI()
  
  engine = engineconn()
  session = engine.sessionmaker()
  
  
  class Item(BaseModel):
      name: str
      number: int
  
  
  @app.get("/")
  async def first_get():
      example = session.query(Test).all()
      return example
  
  
  @app.post("/post")
  async def first_post(item: Item):
      addMemo = Test(name=item.name, number=item.number)
      session.add(addMemo)
      session.commit()
      return item</code></pre>
  </div>
  </div>
  </p>
  <p>Dockerfile</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>FROM python:3.9.0
  
  WORKDIR /home/
  
  RUN echo '2323'
  
  RUN git clone (my git)
  
  WORKDIR /home/(my_folder)
  
  RUN pip install -r requirements.txt
  
  EXPOSE 8080
  
  CMD ["bash", "-c", "gunicorn main:app --worker-class=uvicorn.workers.UvicornWorker --workers=3 --bind 0.0.0.0:8080"]</code></pre>
  </div>
  </div>
  </p>
  <p>docker-compose.yml</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>version: "3.7"
  services:
    nginx:
      image: nginx:1.19.5
      networks:
        - network
      volumes:
        - /home/fastapi_chk/nginx.conf:/etc/nginx/nginx.conf
      ports:
        - 80:80
    fastapi_test:
      image: fastapi_test
      networks:
        - network
    mariadb:
      image: mariadb:10.5
      networks:
        - network
      volumes:
        - mariadb-database:/var/lib/mysql
      environment:
        MYSQL_ROOT_PASSWORD: rkwkrh13!#
      ports:
        - 3306:3306
  
  networks:
    network:
  
  volumes:
    mariadb-database:</code></pre>
  </div>
  </div>
  </p>