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

How do I use async/await with flask-restx? #600

Open
airedwin opened this issue Apr 11, 2024 · 4 comments
Open

How do I use async/await with flask-restx? #600

airedwin opened this issue Apr 11, 2024 · 4 comments
Labels
question Further information is requested

Comments

@airedwin
Copy link

airedwin commented Apr 11, 2024

Ask a question
I have a simple route like

@ns_async.route('/async')
class AsyncResource(Resource):
    async def post(self,):

        await somefunction()

        return jsonify({
            'status': 'success'
        }), 200

when this route is called, i get an error saying that AsyncResource.post wasn't awaited
"RuntimeWarning: coroutine 'AsyncResource.post' was never awaited"

Additional context
the same function works when using normal flask[async]

@flask_app.route('/async', method=["POST"])
async def asyncfunc():

    await somefunction()

    return jsonify({
        'status': 'success'
    }), 200
@airedwin airedwin added the question Further information is requested label Apr 11, 2024
@alpha8133
Copy link

The error you're encountering, "RuntimeWarning: coroutine 'AsyncResource.post' was never awaited," occurs because the async function somefunction() is not being awaited within the post method of the AsyncResource class. To address this issue, you should await the somefunction() call within the post method. Here's the modified code:
@ns_async.route('/async')
class AsyncResource(Resource):
async def post(self,):
# Await the somefunction() call
await somefunction()

    return jsonify({
        'status': 'success'
    }), 200

By incorporating the await keyword before somefunction(), you ensure that the asynchronous operation is properly awaited, resolving the runtime warning and allowing the code to function as expected.

@airedwin
Copy link
Author

airedwin commented Apr 11, 2024

The error you're encountering, "RuntimeWarning: coroutine 'AsyncResource.post' was never awaited," occurs because the async function somefunction() is not being awaited within the post method of the AsyncResource class. To address this issue, you should await the somefunction() call within the post method. Here's the modified code: @ns_async.route('/async') class AsyncResource(Resource): async def post(self,): # Await the somefunction() call await somefunction()

    return jsonify({
        'status': 'success'
    }), 200

By incorporating the await keyword before somefunction(), you ensure that the asynchronous operation is properly awaited, resolving the runtime warning and allowing the code to function as expected.

thanks for the response, but I did await, is it not showing in my example?

the error that i am getting makes me think that whatever flask-restx is handling the call to AsyncResource.post() isn't using await

@airedwin
Copy link
Author

this person at stackoverflow https://stackoverflow.com/questions/68115481/is-it-possible-to-use-flask-restx-wih-flasks-2-0-async-await also asked the same question with no realistic answer

@engFelipeMonteiro
Copy link

related to issue #366

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants