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

apfeltree
  • 11
  • 3
  • Possible duplicate of [What is the current choice for doing RPC in Python?](https://stackoverflow.com/questions/1879971/what-is-the-current-choice-for-doing-rpc-in-python) – ivan_pozdeev May 04 '19 at 20:33
  • Possibly an [XY Problem](http://xyproblem.info/) – jdigital May 04 '19 at 20:35
  • What's the link to RPC here? Could you explain? @ivan_pozdeev – apfeltree May 04 '19 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... – apfeltree May 04 '19 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. – quamrana May 04 '19 at 21:02
  • @apfeltree you said you need one script to call code in another one, while both are running at the sime time. So this is one process calling code in another process. That is called a "remote procedure call". – ivan_pozdeev May 04 '19 at 21:05
  • @quamrana but 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()... – apfeltree May 04 '19 at 21:16
  • when i said this is an XY Problem, i'm referring to the way you asked the question: _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 this assumes a specific implementation. From your other comments, it sounds like you're trying to precompute values that can be used at a later time to speed up a separate program. – jdigital May 05 '19 at 00:44
  • @jdigital Yes, you are true. I want to precalculate some values by A, so that I could use them for several later computations by B and speed up B. Thats the result then. – apfeltree May 05 '19 at 04:07
  • try storing the results in a file, then loading them when you need them. – jdigital May 05 '19 at 04:18
  • @jdigital Thank you for your Idea, but I want to keep the results in the memory, because they are very complex and large. When I load them on every run from file by B I need too long. But when they are precalculated in memory, I have much more time advantage. – apfeltree May 05 '19 at 04:34
  • that's a different question. you've solved the problem (reuse pre-calculated results), now you're trying to make it more efficient. don't assume that the results need to be kept in memory. – jdigital May 05 '19 at 04:40
  • try searching for _python serialization performance_ – jdigital May 05 '19 at 04:50
  • @jdigital no, I haven't solved the reuse precalculated results. I summarize allthing: 1) precalculate big data D with A and keep this D in memory 2) when at a certain time B was started (manually from a user) then B should use the precalculated things D from A and do on top of this another calculation (with using some parameters defined by starting B) 3) D should be allways in memory, because of time advantage. Saving and loading D is no solution for me here. Question is how to use the precalculated D (precalculated by A) within B? – apfeltree May 05 '19 at 04:53

2 Answers2

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

apfeltree
  • 11
  • 3
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))
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 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()... – apfeltree May 04 '19 at 21:23
  • 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? – quamrana May 04 '19 at 21:33
  • 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. – apfeltree May 04 '19 at 22:06
  • I want to achive that the user calls from B are faster because some values are precalculated ;) – apfeltree May 04 '19 at 22:17
  • 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. – quamrana May 05 '19 at 09:08
  • @quarana Because the reading of the file lasts too long. That's no option. Maybe a client-server-construct could help me...I'm trying something – apfeltree May 05 '19 at 09:51