Comments (3)
To be honest this is a pretty bad example to begin with - because you won't notice any difference if you simply remove the lock so it's pretty confusing when users try it out. The stdout is buffered in most environments and won't flush until seeing a newline. We should probably change this to something more obvious - multiple prints maybe (building a triangle?).
The exception mentioned in this issue can be reproduced with spawn, but might be hard to repro with fork as fork is too fast.
from cpython.
Hey @gaogaotiantian , thanks for your reply. I think your point makes sense, and it still confuses me when reading it as well. Either with or without the lock, the example doesn't seem to emphasize what difference this lock brings. Tbh, my original intent was to just complete the example to remove some exceptions, now I think maybe a new example is needed.
from cpython.
one quick example I could think of is as below, we increment a value 3 times in a process.
import multiprocessing
# Function to be executed by each process
def increment_counter(counter, lock, process_id):
lock.acquire()
for _ in range(3):
counter.value += 1
print(f"Counter value: {counter.value} incremented by the {process_id} process")
lock.release()
if __name__ == "__main__":
# Shared counter value
counter = multiprocessing.Value('i', 0)
# Creating a Lock
lock = multiprocessing.Lock()
# Creating processes
processes = []
for j in range(4): # Creating 4 processes
p = multiprocessing.Process(target=increment_counter, args=(counter, lock, j))
processes.append(p)
p.start()
# Waiting for all processes to finish
for p in processes:
p.join()
# Output the final value of the counter
print("Final counter value:", counter.value)
with the lock, the value will consecutively be incremented by the same process, and the example will output something like this
Counter value: 1 incremented by the 1 process
Counter value: 2 incremented by the 1 process
Counter value: 3 incremented by the 1 process
Counter value: 4 incremented by the 0 process
Counter value: 5 incremented by the 0 process
Counter value: 6 incremented by the 0 process
Counter value: 7 incremented by the 2 process
Counter value: 8 incremented by the 2 process
Counter value: 9 incremented by the 2 process
Counter value: 10 incremented by the 3 process
Counter value: 11 incremented by the 3 process
Counter value: 12 incremented by the 3 process
Final counter value: 12
if we remove the lock-related code,
import multiprocessing
# Function to be executed by each process
def increment_counter(counter, process_id):
for _ in range(3):
counter.value += 1
print(f"Counter value: {counter.value} incremented by the {process_id} process")
if __name__ == "__main__":
# Shared counter value
counter = multiprocessing.Value('i', 0)
# Creating processes
processes = []
for j in range(4): # Creating 4 processes
p = multiprocessing.Process(target=increment_counter, args=(counter, j))
processes.append(p)
p.start()
# Waiting for all processes to finish
for p in processes:
p.join()
# Output the final value of the counter
print("Final counter value:", counter.value)
it will randomly print out some duplicates like below, eventually not even reaching 12.
Counter value: 1 incremented by the 3 process
Counter value: 1 incremented by the 2 process
Counter value: 1 incremented by the 0 process
Counter value: 2 incremented by the 1 process
Counter value: 3 incremented by the 3 process
Counter value: 4 incremented by the 2 process
Counter value: 4 incremented by the 0 process
Counter value: 4 incremented by the 1 process
Counter value: 5 incremented by the 3 process
Counter value: 6 incremented by the 2 process
Counter value: 6 incremented by the 0 process
Counter value: 6 incremented by the 1 process
Final counter value: 6
wdyt?
from cpython.
Related Issues (20)
- Use `@file` for gcc commands HOT 11
- New REPL does not include globals from executed module when used with `-i` HOT 3
- Python 3.13.0b2: rounding can cause incorrect log timestamps HOT 1
- Removed unused internal C API functions
- test_email fails on WASI: thread 'main' has overflowed its stack HOT 1
- faulthandler will hang the process with a TSAN and free-thread build Python HOT 5
- cpython HOT 1
- locals() does not include local variables set in exec() statement in 3.13b2 HOT 33
- `datetime.strftime("%Y")` is not padding correctly HOT 10
- psutil fails (segfault) on macOS in the free-threaded (python3.13t) version HOT 3
- Python 3.13 interactive console - CTRL-C does not give control back to gdb HOT 1
- No precise location provided for bytecode of lambda function returning a constant value HOT 2
- New warnings: ``warning: unused function 'is_core_module' [-Wunused-function]``
- Python 3.12.3 -> 3.12.4 breaks use of `create_autospec(X, spec_set=True, name="X")` HOT 2
- some internal compiler functions don't follow the naming convention
- tarfile silently stops/skips on bad member header (e.g. checksum mismatch) HOT 1
- Soft deprecate os.spawn*(), os.popen() and os.system() functions HOT 2
- Emscripten settings in configure.ac are not embedding friendly HOT 15
- Speed up open().read() pattern by reducing the number of system calls HOT 3
- make_ssl_certs fails with "no issuer certificate" with recent openssl HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpython.