Python3.x how to split database connection between processes?

I am running several processes using multiprocessing.Pool

Each process must query my mysql database.

I currently connect to the database once and then exchange the connection between processes

This works, but sometimes I get weird errors. I have confirmed that errors occur when querying the database.

I realized that the problem is that the same connection is used for all processes.

  • Is it correct?

When I searched for the answer, I came across this q & a How to split a single MySQL database connection between multiple processes in Python

So, I was looking for the class pooling.MySQLConnectionPool

If I understand that. I will create a pool with several connections and share the pool between processes. Then each process looks at this pool, and if the connection is available, use it or wait until the connection is released.

  • Is it correct?

But then I found this q & a Access to the MySQL connection pool from Python multiprocessing

At first it seems that the “mate” confirms what I suspected, but at the same time it rejects the use of the pool setting for process sharing

( ) ( , ),

, , .

?

  • ? mysql?

, mata , , init

p = Pool(initializer=init)
  • ? ( ph_singer , )

Pool.map() Pool.map_async() map_async (q, ConnObj) ?

  • ?

,

- , db

. , : qaru.site/questions/1575933/...

, , , . . .

Python, fifos .. .

  • ?

mysql ?

...

+4
1

postgres db Python
, ,

. , , . , , .

, ( q & a)

, , / . , , , -, : IO. - - .

db, ( ) - J.F. Sebastian.

" [db connections] " - , ( / (CPython GIL -)), , db. - . .


, ( postgres db Python)

, . 5 , , . , 100 1000 , ? , , , . - khampson Sep 27 '14 5:19

+3

All Articles