0

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?

asked May 2, 2020 at 16:25
4
  • The main of the the first file is ran is you do python server.py but not if you just import, so funcs is never filled Commented May 2, 2020 at 16:27
  • I always need to have server.py running before anything else. funcs is populated and printed well and if i then run tests.py funcs will be empty there Commented May 2, 2020 at 16:30
  • 1
    If you run the server in a terminal, that fills func in this process, if you import from another run you won't have it Commented May 2, 2020 at 16:31
  • can't I send funcs content to another module then? Or can I make the new module ask for it? Commented May 2, 2020 at 16:54

2 Answers 2

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?

answered May 2, 2020 at 16:31
11
  • Yes. Or just remove the if and let the main prog import server Commented 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. Commented 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? Commented 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 more Commented 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) Commented May 2, 2020 at 17:04
1
  1. 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.
  2. Also, as a good practice you should add a function to server.py to fetch the funcs instead of relying on global.
  3. 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.
answered May 2, 2020 at 16:33
1
  • 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 times Commented May 2, 2020 at 16:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.