Skip to content

Error at threaded_visits.py Ch6, Multiprocessing #14

Description

@binodil

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions