2

For example, let's say I have a function_1 and function_2. Would I be able to run function_1, stop mid-way, wait for function_2 to be done, and then run the rest of function_1 ?

pigaroos
  • 53
  • 5
  • 2
    What event would cause you to stop mid-way? You could just put a call to `function_2` halfway through `function_1`'s code. – Daniel Walker May 24 '20 at 19:32
  • @DanielWalker At least when I tried it, the print statement in `function_1` ran, then `function_2`, then the second `function_1` print statement, but then only after that the `time.sleep(5)` inside `function_2` ran. – TheTechRobo the Nerd May 24 '20 at 19:33
  • 1
    What do you mean by "stop midway"? Do you mean that ``function_1`` itself suspends at a specific point? Do you mean that some third party pauses ``function_1`` (at a given time, instruction, ...?)? Does ``function_1`` know about ``function_2``? Why doesn't ``function_1`` *call* ``function_2`` at the appropriate point? – MisterMiyagi May 24 '20 at 19:34
  • 2
    Would you mind editing your post and showing us at least some pseudo-code for what you're trying to accomplish? – Daniel Walker May 24 '20 at 19:35
  • Does this answer your question? [What is a python thread](https://stackoverflow.com/questions/8623573/what-is-a-python-thread) – smci May 25 '20 at 06:11
  • This is what ***`threading`*** is for. See also [Pausing a thread using threading class](https://stackoverflow.com/questions/3262346/pausing-a-thread-using-threading-class) – smci May 25 '20 at 06:12

3 Answers3

2

Are both functions run under the same process, on the same thread? (if you don't know what this means, they probably are)

If so, there isn't really a good way to do this directly. My advice would be to refactor your code somehow so this isn't necessary, possibly by breaking up functions into smaller functions (so you could run a small function that does the first part of function 1, then run function 2, then run a small function that does the second part of function 1)

Another way would be to call function 2 from within function 1, which would transfer control to function 2 until function 2 returns.

Which one of these is the better choice will depend on the nature of your problem.

EDIT: these of course are not the only ways to do this, (the asker could run the functions on seperate threads, and do explicit synchronization) but these are two fairly simple ways to do what they want.

Trevor Galivan
  • 373
  • 2
  • 13
1

i thing this might help but if you a clear answer please provide an example or an idea of what you trying to make

def func1():
print("func1")

def  func2():
    print("part1")
    func1()
    print("part2")

func2()
0

If the functions are not too complex and arent heavily reliant on efficiency than a crude solution would be to use pythons time module and use time.sleep wherever you need a function to stall..

You could also try calls to function_2 within function_1 these ideas are often used in the programming concept of recursion So my idea would be

def function_1():
#function1 code
function_2()
#the rest of function1's code

def function_2():
#function_2's code

This should follow the flow you want

hope this helps!

Robert O'Brien
  • 133
  • 1
  • 11