0

I have 3 functions in a python script that I would like to run at the same time but from another python script, for example:

def a():
       print("\nphrase1")
def b():
       print("\nphrase2")
def c():
       print("\nphrase3")

I would like to run those 3 functions from a different file. Could anyone support me on that?

8
  • 1
    import the function in all your python script and run it. where is the issue? Commented Aug 8, 2020 at 6:36
  • I want to avoid executing them like this a () b () c (), is there any other more professional way? Commented Aug 8, 2020 at 6:39
  • a(),b(),c() is the only professional way it know in python. I think i am still not clear with the issue Commented Aug 8, 2020 at 6:41
  • do you mean run those functions by 3 threads? At the same time? I do not think it is a good idea if it is not need it. Commented Aug 8, 2020 at 6:43
  • by "at the same time" you mean parallel? Commented Aug 8, 2020 at 6:45

2 Answers 2

1

I suggest you copy the program with the functions to the same folder as the program you want to run them

from yourprogram import a, b, c
#code
a()
b()
c()
Sign up to request clarification or add additional context in comments.

Comments

1

Suppose if all above function is inside module fun.py then use below code snippet to run all of it -

import fun
for i in dir(fun):
    item = getattr(fun,i) 
    if callable(item):
        item()

dir(fun) retrieves all the attributes of module fun. If the attribute is a callable object, call it. Just a note, it will call everything which is callable in the fun module.

Hope this answers your question.

1 Comment

This is a really bad idea, you have no idea what callable attributes the module will have other than the intended functions. For example if it contains from collections import Counter then this will be a callable attribute of the module.

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.