[英]Update variable from imported module with Python when using Threading
在一個項目中,我想將可視化和計算分為兩個不同的模塊。 目的是將計算模塊的變量傳輸到主腳本,以便使用可視化腳本將其可視化。
在這篇文章之后使用文件之間的全局變量? ,我現在可以使用config-script來在腳本之間傳輸變量。 但是不幸的是,在使用線程時這是行不通的。 main.py的輸出始終為“ get:1”。
有人有主意嗎?
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()
好吧,自己找答案。 問題出在“ main.py”中。 必須在啟動線程后放入“ import viz”:
from threading import Thread
from time import sleep
import change
add_Thread = Thread(target=change.add)
add_Thread.start()
import viz
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.