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

Calling async_task from an async function #725

Open
Alain1405 opened this issue Jul 1, 2023 · 2 comments
Open

Calling async_task from an async function #725

Alain1405 opened this issue Jul 1, 2023 · 2 comments

Comments

@Alain1405
Copy link

Alain1405 commented Jul 1, 2023

I use Django Channels to manage websockets. I have configured Django-Q to use Django ORM as a broker.

In the WS message handler I store some data in DB, then I want to perform some async tasks, here is some sample code

async def receive(self, text_data):
  logging.debug(f"Received WS message: {text_data}")
  
  new_answer = Answer(
      answer=answer,
      ...
  )
  await new_answer.asave()
  async_task(add_answer_to_vectorstore, new_answer.id)

On the async_task call I get You cannot call this from an async context - use a thread or sync_to_async.. I could not find any instance of such error for Django-Q but I suspect it's because of using Django ORM as a broker (model.save() cannot be call within an async context, we should use .asave as in my code).
Has anyone else stumbled upon this? Any solution?

@Alain1405
Copy link
Author

Additional info:

  • No code inside add_answer_to_vectorstore is executed
  • sync_to_async(async_task)(add_answer_to_vectorstore, new_answer) works but I don't see any logs in cluster, so I suppose the task is executed synchronously

@scottpashley
Copy link

scottpashley commented Jul 14, 2023

Hi @Alain1405 - you might be more likely to get a response from https://github.com/django-q2/django-q2 which is an active fork of this project

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

No branches or pull requests

2 participants