This is a project template so I can't change much...
I will omit what I believe are irrelevant parts:
#file server.py
import functions
import json
import socket
funcs = {}
class JSONRPCServer:
"""The JSON-RPC server."""
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = None
def register(self, name, function):
"""Registers a function."""
funcs[name] = function
(...)
if __name__ == "__main__":
# Test the JSONRPCServer class
server = JSONRPCServer('0.0.0.0', 8000)
# Register functions
server.register('hello', functions.hello)
server.register('greet', functions.greet)
server.register('add', functions.add)
server.register('sub', functions.sub)
server.register('mul', functions.mul)
server.register('div', functions.div)
print(funcs)
# Start the server
server.start()
Here this will print all my functions inside the funcs dict.
I have another file that needs the contents of funcs but for testing I have this:
#file test.py
from server import funcs
print(funcs)
This prints an empty dictionary. How do I make it so that funcs keeps it's values across these two files?
2 Answers 2
When you run the test.py file, anything within the
if __name__ == "__main__"
of the server.py isn't being run, since it isn't the main file (when you run server.py directly, it IS going into the if __main__
and therefore filling up the funcs dict). Therefore, all those server.register
calls aren't being run when you run the test.py
file, and hence your funcs
dict is empty.
Maybe put that piece of code with all the register calls in a different function, and call that directly?
-
Yes. Or just remove the
if
and let the main progimport server
Pynchia– Pynchia2020年05月02日 16:35:40 +00:00Commented May 2, 2020 at 16:35 -
The principle here and I cannot change that is that server.py will be running. A client.py will connect and do stuff like invoking those functions. All that works because the server knows whats inside funcs and can call them. But I need to have a third module that keeps track of the functions the server register. And this module that i call tests.py here must at all times if called give back the funcs values.walkman– walkman2020年05月02日 16:47:16 +00:00Commented May 2, 2020 at 16:47
-
You're saying that server.py is ALWAYS running, and you need to be able to call another script to know what
funcs
value is?maor10– maor102020年05月02日 17:01:51 +00:00Commented May 2, 2020 at 17:01 -
If so, it's a bit of an issue- basically you have two python processes, and one needs to access a dictionary from the other. You're definitely going to need to change something in the server.py script so that you'll be able to do this (it's pretty shitty looking through the memory of another process). I'd suggest dumping
funcs
to a json (or maybe pickle) that test.py will read- if this sounds good, I can explain moremaor10– maor102020年05月02日 17:03:55 +00:00Commented May 2, 2020 at 17:03 -
yes... server.py is running all the time waiting for client requests, all that works as intended. This tests.py module must run and get all functions that the server currently knows (not just the names but the function object as well)walkman– walkman2020年05月02日 17:04:15 +00:00Commented May 2, 2020 at 17:04
- When you directly run server.py, it prints populated func as
__name__=="__main__"
evaluates to true. This doesn't work when server.py is imported in another module though. - Also, as a good practice you should add a function to server.py to fetch the
funcs
instead of relying on global. - Also, you can refactor the logic inside name==main to a function (eg. start_server) so that any module can start the server by just calling this function.
-
server.py is the first thing to run and keeps running "infinitely". I can't change how the main works. I can only change where the functions get registered to so that i makes possible to call another module that will know whats inside funcs at all timeswalkman– walkman2020年05月02日 16:49:54 +00:00Commented May 2, 2020 at 16:49
python server.py
but not if you just import, so funcs is never filled