Niulu Academy: Pymysql Module of Python

ioeinternet 30/07/2021 2525

1. Basic increasing test operation 2, Python calls stored procedure 3, multithreading implementation mysql access operation

一、基础操作

It is very troublesome every time you connect, use the context management, simplify the connection process.

二、如何python调用 callproc 进行调用储存过程

1. Create a complete MySQL database connection 2. Use the Cursor () to initialize the database cursor 3. Use the cursor to call the CallProc function to add variables that need to be incurred, such as CallProc (Name, Args) name = "proc_user", args = ['21 ', SYH]; 4.Cursor can pass a series of result sets, using Storeresult to get a series of Iterator points to result set 5. Get results with FetChall method

CallProc cannot obtain the OUT and INOUT variables directly, but the variables are available in the server, and the variable can be obtained by @_procname_n, and can be obtained according to the position of the incoming parameters, such as the first SELECT @ _ProcName_0

调用无参存储过程调用有参存储过程三、多线程、多进程操作mysql

1, multithreaded connection:

At first using the Thieading.Thread module, first create a MySQL connection, then by multiple threads to perform specific SQL. But it is found that when it is executed, it is not a report that the mysql connection is turned off, that is, other exception errors. Internet query, because multiple threads cannot share a database connection, there will be unpredictable situations.

Official recommendations use the connection pool module to refer to the practice of others, create a thread connection pool, and create multiple connections at once.

2. Three ways to create a thread pool 1) Over the past: using the ThreadPool module, this is a third-party module of Python, supports Python2 and Python3. ThreadPool is a more old module. Although there are still some people who are in use, they are no longer mainstream, about Python multi-thread, now starting into the future (FUTURE module), the specific use method is as follows:

The results of the operation are as follows:

2) In the future: Using the Concurrent.futures module, this module is a module from Python3, but Python2.7 or more versions can also be installed, and the specific use is as follows:

The results of the operation are as follows:

[Note] Concurrent.futures.threadpoolexecutor, there are two ways when submitting tasks, one is a submit () function, the other is the map () function, the main difference between the two is:

Map can guarantee the order of the output, the order of Submit output is chaos

If the function of the task you want to submit is the same, you can simplify into Map. However, if the submitted task function is different, or the process of execution is abnormal (the problem will be found directly in the implementation of the MAP execution), Submit ()

Submit and MAP parameters are different. Submit needs to submit a target function and the corresponding parameters each time, MAP only needs to submit a target function, and the parameter of the target function is placed in an iterator (list, dictionary).

3) Now?

Here is a problem, the implementation of the above two thread pools is good. The task can only be added once when the thread pool is initialized. So, suppose I have such a need now, I need to run in the thread pool, and then go again Add a new task inside (note, is a new task, not a new thread), how do you do? In fact, there are two ways:

Rewrite ThreadPool or Future function: This method requires the source code of the source module. You must figure out the implementation mechanism of the source module thread pool to correctly rewrite the method according to your needs.

Build a thread pool yourself: This method needs to have a clear understanding of the thread pool, attached to a constructed thread pool instance:

Latest: Apple's self-development 5g Baseband Will Be Used As Soon AS 2023, QUALCOMM's ProSpects Are Worrying, Mediatek Is Implicated

Next: Qiyue Province Custom Synthesis of Mesoporous Composite Fe | Mesoporous Fe3O4 / MnO2 Composite