Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

global variable not accessible withing thread

from threading import Thread
from queue import Queue
import time

def thread1(threadname, q):
    #read variable "a" modify by thread 2
    while True:
        a = q.get()
        if a is None: return # Poison pill
        print a

def thread2(threadname, q):
    a = 0
    for _ in xrange(10):
        a += 1
        q.put(a)
        time.sleep(1)
    q.put(None) # Poison pill

queue = Queue()
thread1 = Thread( target=thread1, args=("Thread-1", queue) )
thread2 = Thread( target=thread2, args=("Thread-2", queue) )

thread1.start()
thread2.start()
thread1.join()
thread2.join()
Comment

PREVIOUS NEXT
Code Example
Python :: idiomatic python 
Python :: substituir valor simbólico por valor real em uma equação Python 
Python :: pandas count zeros in column 
Python :: range function in python use cases 
Python :: django save another class data while saving a class 
Python :: python save base64 temp file 
Python :: get picamera feed 
Python :: ConversionofDatatypes-I 
Python :: tf.stop_gradient in pytorch 
Python :: tuple merging 
Python :: django reverse accessor clashes for abstract class 
Python :: login system read data python 
Python :: python post np.array object 
Python :: reverse row order padnas 
Python :: pyglet key hold 
Python :: alexa in python 
Python :: explore data dataframe pandas 
Python :: apa itu duck typing python 
Python :: python no mathcing key method found 
Python :: pandas iat 0 
Python :: Remove outliers with median value and Capping 
Python :: python loop increment by 2 
Python :: how to connect smartphone camera to opencv python 
Python :: rmtree (remove tree) example 
Python :: Function to stop a while loop 
Python :: how to combine sets using update() Function 
Python :: save PIL image to s3 
Python :: python yield async await thread function 
Python :: operator overloading in python 
Python :: aws django create superuser 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =