[Python]django using multi-process connection MSYQL error

Source: Internet
Author: User

Problem

MySQL Query error occurred

error: (2014"Commands out of sync; you can‘t run this command now")
Inquire

Explanations in the MySQL documentation

If you get Commands out of sync; You can ' t run this command now in your client code, calling client functions in the wrong order.
This can happen, for example, if you is using Mysql_use_result () and try to execute a new query before you have called my Sql_free_result (). It can also happen if you try to execute both queries that return data without calling Mysql_use_result () or mysql_store_re Sult () in between.

The call is in the wrong order, the same connection, the 2 query request, the first request after the issue is not waiting for MySQL to return to issue a second request

Background thinking

My program here is this, in the Django framework to play a timed task, the task has a loop, the main thread loop query MySQL and then in the loop body generated sub-process, the child process also has MySQL query.

I tested the situation of not practical multi-process is not a problem, the use of multi-process will occur this problem.

Compared to the above document, it is not difficult to think, the mistake should be like this

    • Parent process and MySQL build connection A, loop to fork out a sub-process
    • The child process keeps the variables of the parent process, that is, having MySQL connection a
    • Child process to use connection A to query MySQL, the parent process also concurrently use connection A to access MySQL
    • This is easy to appear above the MySQL mentioned situation, the result is an error

Solve

The solution is very easy to think of, that is, when we fork a process, let him get a new and MySQL connection C or D is good,
Results for several tests, get the following scenario.

In the parent process's loop, the MySQL connection is closed before the child process is created, and MySQL is reconnected in the process.

    from django import db    db.close_connection()    p = Process(target=ap5mintes_scan, args=(ac, details, mtime))    p.start()

is actually the state copy problem, originally multiple threads concurrently call a connection also wrong.
After doing a test, multi-process view of MySQL processlist, and indeed use to establish multiple MySQL connections.

Copyright NOTICE: This article is Orangleliu (http://blog.csdn.net/orangleliu/) original article, the article reproduced please declare.

[Python]django using multi-process connection MSYQL error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.