Python creating a shared variable between threads

后端 未结 2 600
挽巷
挽巷 2020-12-01 09:38

I\'m working on a project in Python using the \"thread\" module.

How can I make a global variable (in my case I need it to be True or False) that all the threads in

2条回答
  •  天命终不由人
    2020-12-01 09:45

    With no clue as to what you are really trying to do, either go with nio's approach and use locks, or consider condition variables:

    From the docs

    # Consume one item
    cv.acquire()
    while not an_item_is_available():
        cv.wait()
    get_an_available_item()
    cv.release()
    
    # Produce one item
    cv.acquire()
    make_an_item_available()
    cv.notify()
    cv.release()
    

    You can use this to let one thread tell another a condition has been met, without having to think about the locks explicitly. This example uses cv to signify that an item is available.

提交回复
热议问题