How to use async_to_sync?

I have the following code.

coroutine = self.client.users.by_user_id(user_id).send_mail.post(request_body)
sync_get_data = async_to_sync(coro)

Normally, I would just await .post(request_body) and the mail would be sent, but I have a synchronous Django project, so I cannot. The code above does not work.

The async_to_sync function operates on a function, not on the results of a function call.

In other words, if you have a function:

async def some_function(arg_1, arg_2):
    pass

it would typically be used like this:

async_to_sync(some_function)(arg_1, arg_2)

Where you pass the function to async_to_sync and not the output from the function call - which is what would happen with async_to_sync(some_function(arg_1, arg_2))

So in other words, you probably want to do something like this:
post_func = self.client.users.by_user_id(user_id).send_mail.post
sync_get_data = async_to_sync(post_func)(request_body)

However, to fully answer this, we’d probably need to know what all these objects and methods are that are being called to understand what context in which they are being run.

It would also be helpful if we saw the complete error message with traceback - it’s likely to help identify where the mismatch is happening.