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
  Importing a function from another file runs the old lines also dedesssse 6 349 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  Putting code into a function breaks its functionality, though the code is identical! PCesarano 1 447 Apr-05-2021, 05:40 PM
Last Post: deanhystad
  Picking a function to execute palladium 1 417 Feb-09-2021, 04:47 PM
Last Post: deanhystad
  [split] SyntaxError when trying to execute code on Windows nehaya 2 682 Aug-04-2020, 11:18 AM
Last Post: nehaya
  How to create an Excel app that runs Python? felipe0216 3 813 May-31-2020, 01:19 AM
Last Post: ibutun
  SyntaxError when trying to execute code on Windows Fred0n 2 1,041 Apr-25-2020, 04:30 AM
Last Post: buran
  Python Program Runs in Pycharm but not in Terminal Vbhardwaj2383 2 1,458 Apr-06-2020, 04:41 PM
Last Post: Vbhardwaj2383
  How to execute Python code leemao 2 854 Dec-21-2019, 02:35 PM
Last Post: leemao
  Help to reduce time to execute the code prakash52kar 1 977 Oct-14-2019, 10:56 AM
Last Post: scidam
  scheduled job only runs once Stan2292 4 1,366 Aug-19-2019, 08:18 AM
Last Post: fishhook

Forum Jump:

User Panel Messages

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