Read value from one thread in Python: queue or global variable?

Read value from one thread in Python: queue or global variable?

Content Index :

Read value from one thread in Python: queue or global variable?
Tag : python , By : user119985
Date : November 26 2020, 03:01 PM

this will help First method is OK if you know what you are doing.
More explanation:
class VoltageTask:

    def __init__(self):
        self.lp_voltage = 0
        self.thread = Thread(target=self.update_voltage)

    def update_voltage(self):
        self.lp_voltage = your_function_to_get_voltage()

    def main_thread(self):

    def start(self):

if __name__ == "__main__":
    task = VoltageTask()

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

python 3.2 global variable not updating when its in a thread

Tag : python , By : Jojo
Date : March 29 2020, 07:55 AM
this will help The global declaration must be inside the function where you modify the global variable:
def exit():
    global stopIt
    stopIt = True

changing global variable with thread in Python

Tag : python , By : S Hall
Date : March 29 2020, 07:55 AM
Does that help You need to explicitly declare q as a global. q += 1 confuses the interpreter otherwise.
import threading

q = 0
def f():
    global q
    q += 1
    print q
    threading.Timer(10, f).start()


python global variable inside a thread

Tag : python , By : yarry
Date : March 29 2020, 07:55 AM
will help you GLB is a mutable object. To let one thread see a consistent value while another thread modifies it you can either protect the object temporarily with a lock (the modifier will wait) or copy the object. In your example, a copy seems the best option. In python, a slice copy is atomic so does not need any other locking.
import random
import time
import threading

GLB = [0,0]

#this is a thread 
def t1():
    while True:
        GLB[0] = random.randint(0, 100)
        GLB[1] = 1
        print GLB

#this is a thread 
def t2():
    while True:
        static = GLB[:]
        if static[0]<=30:
            for i in range(50):
                print i," ",static

a = threading.Thread(target=t1)

b = threading.Thread(target=t2)

while True:

python threading why I can use global variable outside the thread only once

Tag : python , By : pepper
Date : March 29 2020, 07:55 AM
it should still fix some issue In your first block of code, you only print the value of a once. However, your threads do continue to update it.
Instead of just print(a) at the end of your code, try printing a in a loop, like this:
import threading,time

a = 1

def update():
    global a
    a += 1
    print ("updating")
    threading.Timer(1, update).start()

while True:

Global variable vs Parameters in a Python thread

Tag : python , By : Janne Laine
Date : March 29 2020, 07:55 AM
it fixes the issue Is not just a matter of threading, but by using parameters your function will work for any combination of parameters data, not just some specific global variables.
Lets say you have this:
def foo(lst, lst_semaphore, dct, dct_semaphore):
threading.Thread(target=foo, args = (lst1, lst_semaphore1, dct1, dct_semaphore1))
threading.Thread(target=foo, args = (lst2, lst_semaphore2, dct1, dct_semaphore1))
threading.Thread(target=foo, args = (lst2, lst_semaphore2, dct2, dct_semaphore2))
def foo():
    global lst1
    global lst_semaphore1
    global dct1
    global dct_semaphore1
def foo2():
    global lst2
    global lst_semaphore2
    global dct2
    global dct_semaphore2

Related Posts Related QUESTIONS :
  • Tensorflow Estimator API save image summary in eval mode
  • How to Pack with PyQt - how to make QFrame/Layout adapt to content
  • How do I get certain Time Range in Python
  • python doubly linked list - insertAfter node
  • Open .h5 file in Python
  • Joining a directory name with a binary file name
  • python, sort list with two arguments in compare function
  • Is it possible to print from Python using non-ANSI colors?
  • Pandas concat historical data using date minus some number of days
  • CV2: Import Error in Python OpenCV
  • Is it possible to do this loop in a one-liner?
  • invalid literal for int() with base 10: - django
  • Why does my code print a value that I have not assigned as yet?
  • the collatz func in automate boring stuff with python
  • How to find all possible combinations of parameters and funtions
  • about backpropagation deep neural network in tensorflow
  • Sort strings in pandas
  • How do access my flask app hosted in docker?
  • Replace the sentence include some text with Python regex
  • Counting the most common element in a 2D List in Python
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • Pandas string search in list of dicts
  • Decryption from RSA encrypted string from sqlite is not the same
  • need of maximum value in int
  • a list of several tuples, how to extract the same of the first two elements in the small tuple in the large tuple
  • Display image of 2D Sinewaves in 3D
  • how to prevent a for loop from overwriting a dictionary?
  • How To Fix: RuntimeError: size mismatch in pyTorch
  • Concatenating two Pandas DataFrames while maintaining index order
  • Why does this not run into an infinite loop?
  • Python Multithreading no current event loop
  • Element Tree - Seaching for specific element value without looping
  • Ignore Nulls in pandas map dictionary
  • How do I get scrap data from web pages using beautifulsoup in python
  • Variable used, golobal or local?
  • I have a regex statement to pull all numbers out of a text file, but it only finds 77 out of the 81 numbers in the file
  • How do I create a dataframe of jobs and companies that includes hyperlinks?
  • Detect if user has clicked the 'maximized' button
  • Does flask_login automatically set the "next" argument?
  • Indents in python 3
  • How to create a pool of threads
  • Pandas giving IndexError on one dataframe but not on another similar dataframe
  • Django Rest Framework - Testing client.login doesn't login user, ret anonymous user
  • Running dag without dag file in airflow
  • Filling across a specified dimension of a numpy array
  • Python populating dataframe in pandas from text files
  • How to interpolate a single ("non-piecewise") cubic spline from a set of data points?
  • Divide 2 integers (leetcode 29) - recursion issue
  • Can someone explain why do I get this output in Python?
  • How do I scrape pdf and html from search results without obvious url
  • Is there a way to automatically make a "collage" of plots with matplotlib?
  • How to combine multiple rows in pandas with shared column values
  • How do I get LOAD_CLASSDEREF instruction after dis.dis?
  • Django - How to add items to Bootstrap dropdown?
  • Linear Regression - Does the below implementation of ridge regression finding coefficient term using gradient method is
  • How to drop all rows in pandas dataframe with negative values?
  • Most Efficient Way to Find Closest Date Between 2 Dataframes
  • Execution error when Passing arguments to a python script using os.system. The script takes sys.argv arguments
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com