Python Forum

Full Version: What's the best way for multiple modules to handle database activity?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When I write to databases I am currently using locks so that other modules don't run into errors. I am new to programming and I was just wondering what other people think the best way to implement multiple modules writing, deleting and amending tables simultaneously?
Multiple modules don't really matter. Do you mean multiple threads, or multiple processes? If multiple processes, do the processes know about each other?
I've been using different modules that both write and edit csv files, database tables etc. I ran into problems and chatgpt said that I should use locks. So I started using locks with the threading module and it's worked well since. I was curious however what other people do and think about the subject.
Locking is not going to help at all unless you have multiple processes or multiple threads.

When you use locking, you check if it is OK to perform an operation. If not you wait a while and check again, hoping that whatever is locking you out has finished and unlocked the resource. Without muli-tasking or multi-processing there is nobody who is going to unlock that resource. Your program will wait forever, blocking the code that might release the resource you are waiting for.

If you are running into problems with a single task program, it is likely you are not releasing resources when you should. For example, you should not make a bunch of changes to your database without committing. You should also not open a database connection and leave it open, assuming that nobody else has made any changes. Connect, query, make any changes you want, commit, close connection. This is best done using a context manager, forcing the closing when you leave the current context.

If multiple processes or tasks have access to shared, mutable resource you have to use some sort of locking mechanism. There really is no choicer.