Python Forum
How to execute code WHILE a function runs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to execute code WHILE a function runs
#1
Hi everyone,
I have some code for a loading bar animation that I would like to run during execution of specific functions. I know some sort of while loop would be used for this, but no matter what I try, it just doesn't do what I want.

Here's the logic I want:

while function1() is running:
      animationFunction()
Here's the animation code:
import time

bar = [
    " [=     ]",
    " [ =    ]",
    " [  =   ]",
    " [   =  ]",
    " [    = ]",
    " [     =]",
    " [    = ]",
    " [   =  ]",
    " [  =   ]",
    " [ =    ]",
]
i = 0

while True:
    print(bar[i % len(bar)], end="\r")
    time.sleep(.2)
    i += 1
I tried a while true, but I'm not sure if or how to say "while function 1 runs" (while function1 true?) in correct python syntax.
Reply
#2
This might give you some ideas
import progressbar
import time
bar = progressbar.ProgressBar(max_value=progressbar.UnknownLength)
for i in range(20):
    time.sleep(0.1)
    bar.update(i)
Reply
#3
You can do this with threading. My logic is 1) start animation in a different thread, 2) run function 1 3) stop animation. Read this page to start learning about threading in python
import threading
import time

event = threading.Event()

def animation():
    bar = [
    " [=     ]",
    " [ =    ]",
    " [  =   ]",
    " [   =  ]",
    " [    = ]",
    " [     =]",
    " [    = ]",
    " [   =  ]",
    " [  =   ]",
    " [ =    ]",
    ]
    
    i = 0
    while True:
        print(bar[i % len(bar)], end="\r")
        was_set = event.wait(timeout=0.2)
        if was_set:
            break
        i += 1

def function1():
    time.sleep(4)

event.clear()
anim = threading.Thread(target=animation)
anim.start()

function1()
event.set()
anim.join() # <-- wait for anim to terminate
print()
Reply
#4
there is no need to reinvent the wheel - there are third party packages available that help create progressbar and also package like click (for creating nice CLI interfaces) offer built-in progressbar
look at this current thread
https://python-forum.io/Thread-How-can-I...y-software
and this tutorial by @snippsat
https://python-forum.io/Thread-tqdm-Prog...mmand-line
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thank you guys for all of the input... super helpful as always! I thought 'there must be packages for this type of thing', but had no idea where to look.
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to do 100 runs simulation based on the current codes? dududada 6 902 Sep-03-2023, 01:43 PM
Last Post: deanhystad
  Another program runs bho68 7 1,143 Nov-08-2022, 08:16 PM
Last Post: bho68
  Can a program execute code in iPython shell and get result? deanhystad 3 1,663 Jun-17-2022, 03:45 AM
Last Post: Larz60+
  Importing a function from another file runs the old lines also dedesssse 6 2,479 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  Putting code into a function breaks its functionality, though the code is identical! PCesarano 1 1,949 Apr-05-2021, 05:40 PM
Last Post: deanhystad
  Picking a function to execute palladium 1 1,575 Feb-09-2021, 04:47 PM
Last Post: deanhystad
  [split] SyntaxError when trying to execute code on Windows nehaya 2 1,961 Aug-04-2020, 11:18 AM
Last Post: nehaya
  How to create an Excel app that runs Python? felipe0216 3 2,203 May-31-2020, 01:19 AM
Last Post: ibutun
  SyntaxError when trying to execute code on Windows Fred0n 2 2,400 Apr-25-2020, 04:30 AM
Last Post: buran
  Python Program Runs in Pycharm but not in Terminal Vbhardwaj2383 2 3,242 Apr-06-2020, 04:41 PM
Last Post: Vbhardwaj2383

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020