Code Monkey home page Code Monkey logo

python-lock-test's Introduction

Task to solve

Create 2 writers and 2 readers services with different period of launching. Writers periodically change two counters the sum of which always should be 100. The readers show that counter’s value. Prove that without synchronization the sum of two counters is not always 100.

Get started

Clone the repo

git clone [email protected]:danielyandev/python-lock-test.git

Install requirements

pip install -r requirements.txt

Prepare environment

Prepare .env file

cp .env.example .env

Prepare counter files

cp counter.txt.example counter1.txt
cp counter.txt.example counter2.txt

Set USE_FILE_LOCK variable in .env file one of these values: {true, 1} to use file locking, otherwise operations with file will be performed without locking

Run the script

python main.py

Results

Running script with and without locking, we can see different results

With locking

Using locks will result in having correct result: each time the sum will be equal to 100, because files are locked during write/read processes and no other process can access it.

lock

Without locking

If we don't use locks we face such cases when file was overwritten by 2 processes simultaneously and the final result can't be predicted.

no_lock

Conclusion

Without synchronization the sum of two counters is not always 100. To have predictable results in multithreading applications, it is important to use file locking.

python-lock-test's People

Contributors

danielyandev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.