Python 线程间共享的全局变量(使用python-osc)
Python global variable shared between threads (using python-osc)
我在这里遇到了线程问题。我需要线程来访问全局变量。
我读过 previous answer 一个类似的问题,我理解 global 关键字的 "power" 以便函数和线程访问到全局变量。
我能够使下面的代码工作,这对我来说非常简单:
# WORKING CODE !!!
from threading import Thread
import sys, time
a = "" #global variable
def thread1(threadname):
global a
while True:
a *= 2
time.sleep(2)
def thread2(threadname):
global a
while True:
a += 1
time.sleep(1)
if __name__ == "__main__":
thread1 = Thread( target=thread1, args=("Thread-1", ) )
thread2 = Thread( target=thread2, args=("Thread-2", ) )
a = 23
thread1.start()
thread2.start()
while True:
print(a)
现在我想要一个 OSC 驱动函数来修改全局变量 a。
我正在使用 python-osc 模块,并在其自己的线程上创建 OSC 服务器 运行。
和以前一样,我已将 a 声明为 mapped 函数中与“/learn 关联的全局变量" OSC 方法。
令我感到奇怪的是,以下代码的行为方式与之前的代码不同。
2018-10-18 编辑,16:14:“a”根本没有增加,我'我看到打印的是
a: 1
连续打印。就好像我们有两个不同的 "a" 值:一个在 OSC 线程内部增加,它不同于主线程的全局“a”。
我做错了什么?
import threading
from time import sleep
from pythonosc import osc_server, dispatcher
OSCaddress = "192.168.1.68"
OSCport = 13000
a = ""
# OSC functions
def menageLearnButton(unused_addr, args, value):
global a
if value == 1:
a += 1
else:
a += 3
if __name__ == "__main__":
# OSC dispatcher to respond to incoming OSC messages
dispatcher = dispatcher.Dispatcher()
dispatcher.map("/learn", menageLearnButton, "learning")
a = 1
# better to run the OSC server on its own thread
# in order not to block the program here
OSCserver = osc_server.ForkingOSCUDPServer((OSCaddress, OSCport), dispatcher)
OSCserver_thread = threading.Thread(target=OSCserver.serve_forever)
OSCserver_thread.start()
while True:
print("a: {}".format(a))
sleep(1)
非常感谢您的支持。
我认为发生的事情是 'ForkingOSCUDPServer' 正在为每个 OSC 请求创建一个新进程,因此 'a' 每次都会重新初始化。如果我将您的代码切换为使用 'ThreadingOSCUDPServer',它似乎具有所需的行为。
我在这里遇到了线程问题。我需要线程来访问全局变量。
我读过 previous answer 一个类似的问题,我理解 global 关键字的 "power" 以便函数和线程访问到全局变量。
我能够使下面的代码工作,这对我来说非常简单:
# WORKING CODE !!!
from threading import Thread
import sys, time
a = "" #global variable
def thread1(threadname):
global a
while True:
a *= 2
time.sleep(2)
def thread2(threadname):
global a
while True:
a += 1
time.sleep(1)
if __name__ == "__main__":
thread1 = Thread( target=thread1, args=("Thread-1", ) )
thread2 = Thread( target=thread2, args=("Thread-2", ) )
a = 23
thread1.start()
thread2.start()
while True:
print(a)
现在我想要一个 OSC 驱动函数来修改全局变量 a。 我正在使用 python-osc 模块,并在其自己的线程上创建 OSC 服务器 运行。 和以前一样,我已将 a 声明为 mapped 函数中与“/learn 关联的全局变量" OSC 方法。
令我感到奇怪的是,以下代码的行为方式与之前的代码不同。
2018-10-18 编辑,16:14:“a”根本没有增加,我'我看到打印的是
a: 1
连续打印。就好像我们有两个不同的 "a" 值:一个在 OSC 线程内部增加,它不同于主线程的全局“a”。
我做错了什么?
import threading
from time import sleep
from pythonosc import osc_server, dispatcher
OSCaddress = "192.168.1.68"
OSCport = 13000
a = ""
# OSC functions
def menageLearnButton(unused_addr, args, value):
global a
if value == 1:
a += 1
else:
a += 3
if __name__ == "__main__":
# OSC dispatcher to respond to incoming OSC messages
dispatcher = dispatcher.Dispatcher()
dispatcher.map("/learn", menageLearnButton, "learning")
a = 1
# better to run the OSC server on its own thread
# in order not to block the program here
OSCserver = osc_server.ForkingOSCUDPServer((OSCaddress, OSCport), dispatcher)
OSCserver_thread = threading.Thread(target=OSCserver.serve_forever)
OSCserver_thread.start()
while True:
print("a: {}".format(a))
sleep(1)
非常感谢您的支持。
我认为发生的事情是 'ForkingOSCUDPServer' 正在为每个 OSC 请求创建一个新进程,因此 'a' 每次都会重新初始化。如果我将您的代码切换为使用 'ThreadingOSCUDPServer',它似乎具有所需的行为。