python全局变量修改_在另一个线程中更改Python中的全局变量

1586010002-jmsa.png

I want change global variable in module1.py by module2.py.

module1.py

#!/usr/bin/env python

import threading

import module2 as module22

import time

values=True

def main():

print "m"

def thread():

while(values):

print "moduel1"

time.sleep(0.50)

print "END PROGRAM"

def change():

print "change"

values=False

if __name__ == "__main__":

t2=threading.Thread(target=module22.main())

t1=threading.Thread(target=thread())

t1.start()

t2.start()

t1.join()

t2.join()

module2.py

#!/usr/bin/env python

import module1 as module11

def main():

print "module2"

module11.change()

if __name__ == "__main__":

main()

When I run sudo python module1.py:

Result is here

module2

change

moduel1

moduel1..

I want get result

module2

change

END PROGRAM

解决方案

You need to declare values as global, if you dont do this, the variable is handled as new local variable.

def change():

global values

print "change"

values=False

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值