Python Forum
What's the best way for multiple modules to handle database activity? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: What's the best way for multiple modules to handle database activity? (/thread-40308.html)



What's the best way for multiple modules to handle database activity? - SuchUmami - Jul-08-2023

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?


RE: What's the best way for multiple modules to handle database activity? - deanhystad - Jul-08-2023

Multiple modules don't really matter. Do you mean multiple threads, or multiple processes? If multiple processes, do the processes know about each other?


RE: What's the best way for multiple modules to handle database activity? - SuchUmami - Jul-08-2023

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.


RE: What's the best way for multiple modules to handle database activity? - deanhystad - Jul-08-2023

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.