How does Django support asynchronous operations

Environment

  • django = “==3.2.15”

  • python3

  • Mysql

  • Uwsgi + Nginx

Demo Code

  • The request is returned immediately, and the database operation is executed by the background thread (I don’t need to pay attention to the thread execution result, as long as it can be executed correctly).
// views function 
  @classmethod
  def _op_function_view(cls, request):
    task_params = get_request_value(request)
    task_params['user'] = request.user
    thread = threading.Thread(target=Task.function,
                              args=(task_params,))
    thread.start()
    return "Task run success"


// modle calss
class Tast:
  //some mysql filed
  create_username = models.CharField(max_length=50, default='system')
  update_username = models.CharField(max_length=50, default='system')
  ....
  
  // task function
  def function(cls,params):
   // 
   ORM logical
   ......

Problem

  • It happens when I execute the code: (2006, ‘MySQL server has gone away’)

  • What should I do to maintain the current logic (keep the task function running in the background and return requests immediately), and ensure that the background threads do not lose sql

I try

  def function(cls,params):
   // 
   django.db.connection.ensure_connection()
   ORM logical
   ......
   django.db.connection.close()


  • But it will still happen:(2006, ‘MySQL server has gone away’)

Welcome @utopia9527 !

No.

Do not do this. Django - especially not as far back as 3.2 - is not designed to allow you to reliably run separate threads.

If you want any operations to continue after the request has returned a response to the browser, you must use something like Celery.

Now having said that, there is a current third-party app, Django Tasks that is working to implement DEP-0014 to provide the background workers that could be used for this purpose. However, from what I can see it’s still in active development and may not be suitable for your use at this time.

Thank you. I have another question that is quite similar to this one (in a completely different scenario).
If my view function does not exit and I wait for the child thread to finish running before exiting, is this allowed

Remember that nothing is returned to the requesting browser until the view returns it, yes. But in that case, you don’t get any benefit by spawning off a separate thread, and you still have potential problems that can be caused by doing this.

Do yourself a favor and simply abandon any ideas of launching independent threads or processes from within a view. You cannot do it reliably. (It is one of those topics that has been discussed here multiple times already. If you search through the forum, you can find these discussions having more details.)

Thank you for your suggestion. (I’ve decided not to do it this way, "not to create unnecessary trouble for myself. ")

But what if it’s a ‘Coroutine’

What is your ultimate objective? (What is the issue that you’re trying to resolve?) It seems to me like you’re trying to find work-arounds for an already-answered and resolved issue.

Because I have some time-consuming tasks (possibly up to half an hour), which are triggered only through the page and executed in the background and quickly inform the operator that the trigger was successful

Use Celery. (Or some other external processing task.)

I see. thanks ~~~~~~~