Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Error at threaded_visits.py Ch6, Multiprocessing #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AbdirayimovS opened this issue Apr 22, 2024 · 0 comments
Open

Error at threaded_visits.py Ch6, Multiprocessing #14

AbdirayimovS opened this issue Apr 22, 2024 · 0 comments

Comments

@AbdirayimovS
Copy link

Hi there,

Concurrency is great topic. I followed the code regarding threaded_visits.py. Authors argued that when I will run the file, I will get unexpected results. However, I am getting expected 10_000_000 value for thread_visits.

My hypothesis: There might be changes in threading standard library.
Looking for your reply!

from threading import Thread

thread_visits = 0

def visit_counter():
    global thread_visits 
    for i in range(100_000):
        # value = thread_visits
        thread_visits +=  1

if __name__ == "__main__":
    thread_count = 100
    threads = [
        Thread(target=visit_counter)
        for _ in range(thread_count)
    ]
    for thread in threads:
        thread.start()
    
    for thread in threads:
        thread.join()
    print(f"{thread_count=}, {thread_visits=}")
(my_bert) (base) sardor@Sardors-MacBook-Pro Concurrency % python3 thread_chaos.py
thread_count=100, thread_visits=10000000
(my_bert) (base) sardor@Sardors-MacBook-Pro Concurrency % python3 thread_chaos.py
thread_count=100, thread_visits=10000000
(my_bert) (base) sardor@Sardors-MacBook-Pro Concurrency % python3 thread_chaos.py
thread_count=100, thread_visits=10000000
(my_bert) (base) sardor@Sardors-MacBook-Pro Concurrency % python3 thread_chaos.py
thread_count=100, thread_visits=10000000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant