Docker Community Forums

Share and learn in the Docker community.

Issues for implementing before_request decorator in Flask docker_container

HI ,
i am using docker compose in order to create an Service Oriented architecture for a virtual drone .
I am facing some difficulties . Since i have to make the connection with the drone , using Flask it is recommended to use the before_request decorator , this last one can be also use to make the connection for the database or any operation that should be done before any request .
I tried my code with Docker and without :
1-without docker there is no issues the request is working and i am able to send command to the drone
2-when i run the code into the docker image the request is partially working , and i am not able to fulfill the operation or some commands no more . For example i can even print a string message

here is my code
precision it works well without docker :

from dronekit import connect ,VehicleMode,LocationGlobalRelative
from flask import Flask, request
import time,socket,threading,json

global varaibale that will be use for handling the drone object

drone=None

class Drone:
def init(self,vehicle):
self.vehicle = vehicle

def set_vehicle_mode(self,mode):
    self.vehicle.mode=VehicleMode(mode)
    time.sleep(1)
    return "vehicle mode changed with success"

def get_vehicle(self):
    return self.vehicle

def get_vehicle_mode(self):
    return self.vehicle.mode.name

app = Flask(name)
app.secret_key=“dronekit”

Threads that will be used to handle some request

t1=None

@app.before_request
def func():
print("this must run before any request ")

@app.route(’/index’)
def index():
return “welcome to dronekit sitl service”

@app.route(’/test’)
def test():
return “welcome to dronekit sitl 4444 service”

if name == ‘main’:
app.run(host=‘0.0.0.0’,debug=True)

1 Like

I am having the same issue, were you ever able to find the fix?