In a project, I would like to separate the visualization and calculation in two different modules. The goal is to transfer the variables of the calculation-module to a main-script, in order to visualize it with the visualization-script.

Following this post Using global variables between files?, I am able to use a config-script in order to transfer a variable between to scripts now. But unfortunately, this is not working when using threading. The Output of the main.py is always "get: 1".

Does anyone have an idea?

main.py:

from threading import Thread
from time import sleep

import viz

import change
add_Thread = Thread(target=change.add)
add_Thread.start()

viz.py:

import config

from time import sleep

while True:
    config.init()
    print("get:", config.x)
    sleep(1)

config.py:

x = 1

def init():
    global x

change.py:

import config

def add():
    while True:
        config.x += 1
        config.init()
share|improve this question

OK, fount the answer by myself. Problem was in the "main.py". One has to put the "import viz" after starting the thread:

from threading import Thread
from time import sleep

import change
add_Thread = Thread(target=change.add)
add_Thread.start()

import viz
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.