I created script for fetching file from net, but I want to add some animation during fetching process. I use multiprocessing for this, because fetch script pauses program until the file is downloaded.
In req() function I will put some code that fetches my url, it works nice, but animation doesn't stop when file is downloaded.
In anim() there is some animation script, it should check s variable every symbol changing time and if s = False animation need to stop. But in anim(), s is always True, it doesn't updates, s = False assignment in req() is ignored!
How to update s in anim()?
import time
import requests
import sys
import multiprocessing
animation = "|/-\\"
s = True
def req(): # Here I'll write some other code, but 's' variable will be False
global s
time.sleep(3)
s = False
def anim():
i=0
global s
while s == True:
time.sleep(0.1)
sys.stdout.write("\r" + animation[i % len(animation)])
sys.stdout.flush()
i+=1
p1 = multiprocessing.Process(name='p1', target=anim)
p = multiprocessing.Process(name='p', target=req)
p1.start()
p.start()
P.S. You should run this code snippet from Terminal, because it uses flush()