0

I want to call a method in running python script from another script.

For eg:

script1.py is continuously running. In this script there are methods.

From another script2.py i want to call method of script1.py

6
  • 1
    Are you asking about RPC? Commented Nov 5, 2014 at 6:35
  • 'method of script1.py': Do you want to call the method of the running instance or of a new instance? Commented Nov 5, 2014 at 6:41
  • @kums from running instance. Commented Nov 5, 2014 at 6:49
  • Why can't you just import methods of script1.py in script2.py.? Commented Nov 14, 2014 at 8:37
  • 1
    Then I believe ans suggested by @Dunes will do the trick. OR can you elaborate more with code? Commented Nov 14, 2014 at 11:08

2 Answers 2

3

What you want is the multiprocessing module and a Manager object. Create a Manager object in script1.py, register the methods you want to call, and then start the server.

The module documentation has a good example of how to do all this.

https://docs.python.org/3/library/multiprocessing.html#multiprocessing-managers

Specific examples on how to use a remote manager can be found here.

Sign up to request clarification or add additional context in comments.

Comments

-1

You can import sript1.py and use function from it, because the script1.py is cached by interpreter. If I understand the question correctly.

1 Comment

So, if you import - import script1, you don't prevent to the running script1.py.

Your Answer

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.