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?

yes i fixed it in the meanwhile. address me your method and i will find a way to help you

If you try to access request, or anything that uses it, outside a request context, you’ll get this error message:

RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that
needed an active HTTP request. Consult the documentation on testing
for information about how to avoid this problem.
This should typically only happen when testing code that expects an active request. One option is to use the test client to simulate a full request. Or you can use test_request_context() in a with block, and everything that runs in the block will have access to request, populated with your test data.

def generate_report(year):
format = request.args.get(‘format’)

with app.test_request_context(
‘/make_report/2017’, data={‘format’: ‘short’}):
generate_report()