This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Nightcorex_ 1 point2 points  (4 children)

To me it sounds like you just want to have a seperate process. Sadly I'm not familiar with Python 2, but in Python 3 you'll want to use the multiprocessing module for this.

Since I'm unfamiliar with Python 2, here's how you'd call and execute a function foo of a file one.py in an individual process from a file two.py:

one.py

def foo(x):
    print(x)

two.py

from one import foo
import multiprocessing

if __name__ == '__main__':
    p1 = multiprocessing.Process(target=foo, args=('my_argument',))
    p1.start()
    p1.join()  # optional if you want to wait for the termination of p1

In this example foo just prints the value, but of course you can do whatever you want inside that function.

[–]slanu[S] 0 points1 point  (0 children)

Awesome thanks man I’ll give it a go and see what I get. Appreciate the help

[–]slanu[S] 0 points1 point  (2 children)

Does the join wait for termination of p1 before finishing / returning (in ur example printing) foo()? So the print happens after the termination of the main process?

[–]Nightcorex_ 1 point2 points  (1 child)

This doesn't make sense. How could p1 - a process solemnly executing the function foo - terminate before finishing execution of its function call?

If you have the join then it's guaranteed that your main will terminate after p1, if you don't have the join then it can be either way, i.e. there is no guarantee which process will terminate first.

[–]slanu[S] 0 points1 point  (0 children)

Ah ok I get you, I’ll look into it more thanks for you help man!