0

It's little hard to explain for me, but I have a python-script (A) with some code-part, which runs on start and initializes some things and variables and then the script (A) should wait...

Script (A) has also a function (f) which uses the initialized things and variables.

I want to call this function (f) from another script (B) then.

But my problem is, that script (A) closes after the init-part, because the script is finished. It's plausible for me, that it do so, but how could I make it wait for the call from another script (B).


Example could be: Script A:

# init-part
if __name__ == '__main__':  # file is executed
    x = 3*4
# end init-part

else:
    def f():
        return x+5

Script B:

import filenameA

# call funtion from A, which uses the preinitalized variable x
y = filenameA.f()
print('y=' + str(y))

The init-part for demonstration is here quite simple, but in real it's more complicated, but for my problem it's not necessary now. That's it.


I think it's quite simple, I want to start script A via console 'python filenameA.py' and it should init and wait for the function call, when I start 'python filenameB.py' from another console.

But script A closes after init... A loop for waiting uses CPU-time, that's not what I want.

I don't know how to search properly for solutions to this, because it's quite hard for me to find the right keywords -.- I hope you understand what I want to achieve =)

I'm thankfully for any help ;) apfeltree

14
  • Possible duplicate of What is the current choice for doing RPC in Python? Commented May 4, 2019 at 20:33
  • Possibly an XY Problem Commented May 4, 2019 at 20:35
  • What's the link to RPC here? Could you explain? @ivan_pozdeev Commented May 4, 2019 at 20:51
  • @jdigital no i have no other problem xy i think. I only want to make use of the more time-intensive init-part in later function-calls, without doing the init-part again... Commented May 4, 2019 at 20:53
  • You just need to have script A call script B. That way A can initialize things, wait for B by calling B and B can call something from A. Commented May 4, 2019 at 21:02

2 Answers 2

1

Ok, it's done now. I have implemented in A a server and in B a client. So at the beginning the user starts A and A makes the precalculation and then starts the server and waits for some clients... When the user starts B then it connects to A, transfers its parameters and A does a calculation based on the precalculation and answers B the solution. (y) --> well done for me

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

Comments

0

Script A

from B import b
x = 0

def f():
    return x + 5

def init():
    global x
    x = 3 * 4

if __name__ == '__main__':  # file is executed
    init()
    b(f)
# end init-part

Script B:


def b(f):
    # call funtion from A, which uses the preinitalized variable x
    y = f()
    print('y=' + str(y))

6 Comments

B should not be called directly after A. There is some user, which manually starts B at a random time, when the user needs some calculation from f()...
Do you mean that when a user starts A, then the values calculated might be different each time, or possibly might take a long time to calculate, so it needs A to have precalculated them in advance, and will wait for perhaps multiple users running B?
Yes, we are on the right track now: A should precalculate values, which are not different each time, but the calculation needs some time. And a user could start with B the function f() (with some parameters which could differ earch call) which calculates then a result, but needs not so much time, because some values are precalculated by A. And the user could start this calculation several times, and each time the precalculated values should be used - they stay the same.
I want to achive that the user calls from B are faster because some values are precalculated ;)
Why not save results from running script A to a file and quit. Later script B runs, calls some functions from A which read the file and return the data.
|

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.