我写了这个最小的代码来解释我的案例:
import threading
import time
import eventlet
from eventlet import backdoor
eventlet.monkey_patch()
global should_printing
should_printing = True
def turn_off_printing():
global should_printing
should_printing = not should_printing
def printing_function():
global should_printing
while should_printing == True:
print "printing"
time.sleep(1)
def console():
while True:
print "inside console"
time.sleep(1)
if __name__ == '__main__':
eventlet.spawn(backdoor.backdoor_server, eventlet.listen(('localhost', 3000)))
thread = threading.Thread(target=printing_function)
thread.start()
thread = threading.Thread(target=console)
thread.start()在执行之后,我通过telnet连接,导入我的模块并调用turn_off_printing()。但它不起作用。是我搞错了,还是不可能?
发布于 2012-01-28 05:35:32
正如fthinker在上面的评论中所说的:
看起来后门服务器并不使用相同的名称空间。键入函数名称会显示它们是未定义的,您的变量'should_printing‘也是未定义的。我在远程登录到后门服务器设置的解释器时对此进行了测试。
(如果fthinker回复为回复帖子,我将删除此帖子)
发布于 2015-05-18 19:43:53
确保在调用后门时传递所有想要访问的变量/函数
if __name__ == '__main__':
s=eventlet.spawn(backdoor.backdoor_server, eventlet.listen(('localhost', 3000)), globals())
thread1 = threading.Thread(target=printing_function)
thread1.start()
s.wait()现在,在端口3000上运行的python解释器上应该可以看到should_printing,将其设置为false将停止打印
发布于 2014-09-04 14:23:12
您无法访问should_printing,因为__main__模块与导入的模块不同,即使它们是相同的模块。检查详细信息here
the executing script runs in a module named __main__, importing the
script under its own name will create a new module unrelated to
__main__.https://stackoverflow.com/questions/9028423
复制相似问题