apfeltree
apfeltree

Reputation: 11

How to run some code in python script, then pause script and wait for function call from another script

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

Upvotes: 0

Views: 300

Answers (2)

apfeltree
apfeltree

Reputation: 11

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

Upvotes: 1

quamrana
quamrana

Reputation: 39404

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))

Upvotes: 0

Related Questions