简体   繁体   English

mysql.connector SELECT查询是同时运行还是顺序运行?

[英]Are mysql.connector SELECT queries run concurrently or sequentially?

I'm currently using mysql.connector in a python Flask project and, after users enter their information, the following query is executed: 我当前在python Flask项目中使用mysql.connector ,在用户输入信息后,将执行以下查询:

"SELECT first, last, email, {} FROM {} WHERE {} <= {} AND ispaired IS NULL".format(key, db, class_data[key], key)

It would pose a problem if this query was executed in 2 threads concurrently, and returned the same row in both threads. 如果此查询同时在2个线程中执行,并且在两个线程中返回相同的行,则会造成问题。 I was wondering if there was a way to prevent SELECT mysql queries from executing concurrently, or if this was already the default behavior of mysql.connector ? 我想知道是否有一种方法可以阻止SELECT mysql查询并发执行,或者这是否已经是mysql.connector的默认行为? For additional information, all mysql.connector queries are executed after being authenticated with the same account credentials. 有关更多信息,所有mysql.connector查询都将在使用相同的帐户凭据进行身份验证之后执行。

It is hard to say from your description, but if you're using Flask, you're most probably using (or will use in production) multiple processes, and you probably have a connection pool (ie multiple connections) in each process. 从您的描述很难说,但是如果您使用Flask,则很可能正在使用(或将在生产中使用)多个进程,并且每个进程中可能都有一个连接池(即多个连接)。 So while each connection is executing queries sequentially, this query can be ran concurrently by multiple connections at the same time. 因此,当每个连接顺序执行查询时,可以同时由多个连接同时运行此查询。

To prevent your application from obtaining the same row at the same time while handling different requests, you should use transactions and techniques like SELECT FOR UPDATE . 为了防止您的应用程序在处理不同的请求时同时获取同一行,应使用事务和技术,例如SELECT FOR UPDATE The exact solution depends on your exact use case. 确切的解决方案取决于您的确切用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM