Multiple processes accessing a shared resource concurrently
This example starts a shared database and multiple processes.
The processes represent multiple users, or locations, or programs hitting a shared database at the same time.
- Git
- Python 3.7+ (3.11+ preferred)
- VS Code Editor
- VS Code Extension: Python (by Microsoft)
Fork this repository ("repo") into your GitHub account.
Clone your new GitHub repo down to the Documents folder on your local machine.
Explore your new project repo in VS Code on your local machine.
Execute 00_check_core.py to generate useful information.
Execute multiple_processes.py.
Read the output. Read the code. Try to figure out what's going on.
- What libraries did we import?
- Where do we set the TASK_DURATION_SECONDS?
- How many functions are defined?
- What are the function names?
- In general, what does each function do?
- Where does the execution begin? Hint: generally at the end of the file.
- How many processes do we start?
- How many records does each process insert?
In this first run, we start 3 processes, each inserting 2 records into a shared database (for a total of 6 records inserted.)
In each case, the process gets a connection to the database, and a cursor to execute SQL statements. It inserts a record, and exits the database quickly.
In general, we're successful and six new records get inserted.
For the second run, modify the task duration to make each task take 3 seconds. Hint: Look for the TODO. Run the script again. With the longer tasks, we now get into trouble - one process will have the database open and be working on it - then when another process tries to do the same, it can't and we end up with errors.
To clear the terminal, in the terminal window, type clear and hit enter or return.
clear
To document results, clear the terminal, run the script, and paste all of the terminal contents into the output file.
Use out0.txt to document the first run.
Use out3.txt to document the second run.
To get more help on the early tasks, see streaming-01-getting-started.
On Windows the select all, copy, paste hotkeys are:
- CTRL a
- CTRL c
- CTRL v
On a Mac the select all, copy, paste hotkeys are:
- Command a
- Command c
- Command v
Detailed copy/paste instructions (as needed)
- To use these keys to transfer your output into a file, clear the terminal, run the script, then click in the terminal to make it active.
- To select all terminal content, hold CTRL and the 'a' key together.
- To copy the selected content, hold CTRL and the 'c' key together.
- To paste, open the destination file (e.g. out0.py) for editing.
- Click somewhere in the destination file to make it the active window.
- Now hit CTRL a (both together) to select all of the destination file.
- Hit CTRL v (both together) to paste the content from your clipboard.
Do a web search to find helpful videos on anything that seems confusing and share them in our discussion.
Python has pretty helpful error messages. When you get an error, read them carefully.
- What error do you get?
Do a web search on the sqlite3 'database is locked' error.
- What do you learn?
- Once a process fails, it crashes the main process and everything stops.
Deadlock is a special kind of locking issue where a process is waiting on a resource or process, that is waiting also.
Rather than crashing, a system in deadlock may wait indefinitely, with no process able to move forward and make progress.
Check out Wikipedia's article on deadlock and other sources to learn how to prevent and avoid locking issues in concurrent processes.