24th May 2025, 1:11 PM
Bob_Li
Bob_Li - avatar
+ 5
Jan I've never really delved into Python's threading and multiprocessing before, too. So it's also a great learning opportunity for me. Mihir Dev's code was a great starting point. It looks like it could work, but it didn't, so that's interesting. Shared objects is a major pain point in threading and multiprocessing, so I concentrated on that. and yes, the thread-safety warning light was flashing in my head when I first read the code. where's the locks?
24th May 2025, 11:41 PM
Bob_Li
Bob_Li - avatar
+ 4
from https://docs.python.org/3.6/library/multiprocessing.html#multiprocessing.Value "Operations like += which involve a read and write are not atomic. So if, for instance, you want to atomically increment a shared value it is insufficient to just do counter.value += 1 Assuming the associated lock is recursive (which it is by default) you can instead do with counter.get_lock(): counter.value += 1 " ??? I get lost in cryptic documents too... what associated lock ?... further reading gives: "If lock is True (the default) then a new recursive lock object is created to synchronize access to the value. If lock is a Lock or RLock object then that will be used to synchronize access to the value. If lock is False then access to the returned object will not be automatically protected by a lock, so it will not necessarily be “process-safe” so maybe a new lock object is created each time, unless you set lock to False or is using a global lock ...
24th May 2025, 1:36 PM
Bob_Li
Bob_Li - avatar
+ 3
Jan there's never a need to solve a problem quickly for me. i like to think slowly. maybe that's why i avoid the code challenges. it's even better if it's a problem that I can't solve. Then it means someone will be able to solve it and I learn something new, or I get to look up and try out new things. with the push to remove the GIL in python, I hope Python's threading and multiprocessing capabilities will see new improvements.
25th May 2025, 7:52 AM
Bob_Li
Bob_Li - avatar
24th May 2025, 7:10 AM
Jan
Jan - avatar
+ 2
Bob_Li I think you used a little bit more time to research the problem than I did. I think I forgot that the thread-safe counter was the pivotal point in that task and not only about threads.
24th May 2025, 6:20 PM
Jan
Jan - avatar
+ 2
0mi patil your answer is technically the correct one... Mihir Dev was asking about multithreading, but his example was multiprocessing, not multithreading. I didn't notice that ... 😅 your solition is multithreading.👍 here's a comparison (I used Omi patil's code for the multithreading part) notice the process id and thread numbers : https://sololearn.com/compiler-playground/chWUVUHYNGU8/?ref=app
26th May 2025, 10:03 AM
Bob_Li
Bob_Li - avatar
+ 2
if you don't mind globals, here's a simpler way to share and lock objects in threads https://sololearn.com/compiler-playground/cl56XE68Tca1/?ref=app
26th May 2025, 2:04 PM
Bob_Li
Bob_Li - avatar
+ 1
this is not a homework writing service. show your code where did you get stuck. we are here and ready to help you
23rd May 2025, 7:36 PM
Mihaly Nyilas
Mihaly Nyilas - avatar
+ 1
I made a little test of your code, and it seems that your counter is resetting to 0 every time the Process is called.
23rd May 2025, 9:17 PM
Jan
Jan - avatar
+ 1
Bob_Li Normally, when I see a users problem, I say to myself - can I solve this within minutes with a quick solution! But then it happens I either misunderstand the users problem, or I have to realize that it's going to take more time to research than I expected. Oh no, I'm on a mobile with a complicated problem. That's not good, because I cannot use hours on that now, especially if I have do dive into something that I normally don't deal with.
25th May 2025, 7:40 AM
Jan
Jan - avatar
+ 1
Solution received?
26th May 2025, 5:18 PM
Hariharan S (Digital Marketing Link In Bio)
Hariharan S (Digital Marketing Link In Bio) - avatar
+ 1
26th May 2025, 10:08 PM
Hariharan S (Digital Marketing Link In Bio)
Hariharan S (Digital Marketing Link In Bio) - avatar
0
.
25th May 2025, 2:41 PM
(⁠。⁠♡⁠‿⁠♡⁠。⁠)✧(⁠>SAD CODDER<⁠)✧(⁠。⁠♡⁠‿⁠♡⁠。⁠)
(⁠。⁠♡⁠‿⁠♡⁠。⁠)✧(⁠>SAD CODDER<⁠)✧(⁠。⁠♡⁠‿⁠♡⁠。⁠) - avatar