1

I'm working on a thread that updates a variable of the main process. As I did not succeed passing arguments I had to use global. But I don't like it, it's not safe. There is any way without using global?

I can pass the argument, but my issue is with the returning object.

My code:

#!usr/bin/python3
# -*- coding: UTF-8 -*-

import threading
import time

my_check = " CHECK "    # TODO replace with initial source data.

class MiThread(threading.Thread):   
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):  
        time.sleep(5)
        print("I'm the SECOND thread")
        global my_check
        my_check = " CHECK DONE!!!" # TODO replace with the updated source data.
        self.run()

print ("I'm the principal thread.")

t = MiThread()     
t.start()       
#t.join(1)        

while True:
    time.sleep(0.5)
    print("I'm the principal thread, too." + my_check)

This is only a concept proof, really I want to code a little stocks ticker displayer in a tkinter label, and I need, at least two more threads: the updater thread (for update the information to display) and the displayer thread (it must changes the text of the label each 0.3 seconds).

1 Answer 1

1

You can store result of thread execution in instance variable of thread object:

import threading
import time

class MiThread(threading.Thread):   
    def __init__(self):
        self.my_check = " CHECK "
        threading.Thread.__init__(self)

    def run(self):  
        time.sleep(5)
        print("I'm the SECOND thread")
        self.my_check = " CHECK DONE!!!"

print ("I'm the principal thread.")

t = MiThread()     
t.start()         

while True:
    time.sleep(0.5)
    print("I'm the principal thread, too." + t.my_check)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.