Can I call functions or change variables throught eventlet.backdoor in Python?
Question
I wrote this minimal code to explain my case:
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()
After executing it, I connect through telnet, import my module and call turn_off_printing(). But it not works. Do i made mistake, or it is not possible?
Solution 3
As fthinker said in comment above:
It doesn't look like the backdoor server is using the same namespace. Typing the function names said they were undefined and your variable 'should_printing' is undefined as well. I tested this while telnetted into the interpreter set up by the backdoor server.
(if fthinker reply as answer post i will delete this post)
OTHER TIPS
Make sure you pass all the variables/functions you like to access while invoking backdoor
if __name__ == '__main__':
s=eventlet.spawn(backdoor.backdoor_server, eventlet.listen(('localhost', 3000)), globals())
thread1 = threading.Thread(target=printing_function)
thread1.start()
s.wait()
Now should_printing should be visible on the python interpreter running on port 3000 and setting it to false would stop printing
You can't access should_printing
because the __main__
module is different from the module imported, even if they are the same module. Check the detail 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__.