Python Forum
What's the best way for multiple modules to handle database activity?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's the best way for multiple modules to handle database activity?
#1
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?
Reply
#2
Multiple modules don't really matter. Do you mean multiple threads, or multiple processes? If multiple processes, do the processes know about each other?
Reply
#3
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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get Azure activity logs using python script raham3406 4 3,596 Apr-27-2021, 05:10 AM
Last Post: raham3406
  Pythonic way to handle/spread alerts class in multiple modules psolar 11 4,633 Feb-12-2020, 04:11 PM
Last Post: psolar
  Modules issue, pip3 download modules only to pyhton3.5.2 not the latest 3.6.1 bmohanraj91 6 8,464 May-25-2017, 08:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020