Multi Workers. Are they safe to use with mysql.connector.threadsafety = 1

Hi,

My team picked up sanic for a new project.
We are really keen to utilise the performance boost that async can provide.

One of this concerns we have is the use of “mysql.connector.threadsafety = 1” with multiple workers.
Could someone share more context on how workers are created ?

If all workers are real processes, then using “mysql.connector.threadsafety = 1” should be safe.
However, if the workers are actually threads, the use of “mysql.connector.threadsafety = 1” will be a major concern, because there’s no locking to ensure thread safety.

More on mysql python connector thread safety:

Has anyone done any test / experiement on this?

Using mysql/mariadb with python is popular. I bet I’m not the first one.

Thanks ahead.

Sorry I did not respond earlier. I saw the question was about mysql and somewhat ignored it since I never work with it. Therefore, my response will be more general in terms of how Sanic works with DBs in general, not just MySQL.


When you run Sanic with workers=n there are no threads involved. In fact, there is never a use of threading with Sanic, and for the most part I would discourage it. Mixing threads and asyncio can be a bit of a mess, so unless you have a specific need for it, I would discourage it.

So, what happens with workers=n? You will end up with n+1 processes. There is a main process that would (for example) respond to CTRL+C commands. Then, each of n workers will be forked using multiprocessing. They exist wholly on their own without any shared resources.

I hope this answers your question.