Replies: 1 comment 2 replies
-
In general, asyncio is intended to simplify the code needed to handle multiple concurrent activities. Yes, you can use threads with sync mode in a very similar fashion, but it is far easier to mess up. You will also be stuck with the same problems that you suggested would occur for using asyncio! If you have a pool of 5 connections, the first 5 will proceed and do their work while the other 5 will "hang" (wait) for those first 5 to be completed. And the main drawback to using threads with sync mode is that those threads cannot do any useful work while they are waiting -- either for a connection to be available in the pool or for the database to complete its work. If you have a lot of I/O in your application then asyncio will work well and be highly efficient. Using threads with sync mode in that situation will not improve things -- if you think otherwise, please let me know! If you have a lot of CPU intensive work, that should be done outside the event loop or you should avoid using asyncio completely. The scenario you have discussed, however, is not one that is a "problem" for asyncio. Perhaps I am missing something? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a fundamental question about using oracledb with async python.
Oracle connections are not thread safe. Two threads/coroutines cannot execute queries concurrently, whether you are in a transaction or not.
If we have a pool of 5 connections, and then 10 HTTP requests arrive simultaneously and are processed as coroutines on an event loop, and all 10 request an async oracle connection from the pool, only the first 5 will win, and the remaining 5 connections will hang until the connections have been returned to the pool.
Given this, I'm struggling to understand in what cases you would want to use an async connection in an event loop. It seems to me that in the common case, using threads with sync python should be adequate.
I suppose you could at the most basic level simply crank up the number of connections in your pool to some very high number, say 1000 connections. Oracle can handle 1000 open connections.
Is this what everyone who uses async connections is doing?
If your use case allows it, you could take a hot path API and instead of holding a connection open for the entirety of the API call, you could return a connection to a pool as soon as you are done executing a query, and then request a new connection when you need to execute a new query. For example if you had some hot path that was read only and didn't use a connection, and executed 5 queries over the course of the hot path, you could open and close a connection 5 times.
And if your hot path API has writes and if your use case permits it, you could commit after each write/use pragma autonomous transaction, return the connection to the pool, and then request a new one for the next write.
Are there any other strategies to consider?
Beta Was this translation helpful? Give feedback.
All reactions