Django How to deal with database locking
I have two functions, periodic_signals()
, and update_choice()
, and use postgres
.
update_choice()
is called once users make a new choice, and update the database.
periodic_signals()
is called every 0.1s using Threading.timer
, and reads users' choices from the database, and do some stuffs, then send signals back to users, and plot them in real time.
When periodic_signals()
is called every 1s, I don't have any problems. When periodic_signals()
is called every 0.1, everything I call update_choice()
, there is a pause in my plot, and I can see the execution time of update_choice()
increases 10x.
Is it because I read and write to the same row almost at the same time, causing database locking? How can I solve it, except to improve the performance of these two functions (at the moment both function take about 0.005-0.02s depending on the number of users)?