0

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()

  • 1
    Possible duplicate of [Python multiprocessing global variable updates not returned to parent](https://stackoverflow.com/questions/11055303/python-multiprocessing-global-variable-updates-not-returned-to-parent) – kabanus Apr 30 '18 at 10:28
  • The highest voted answered I think is good : https://stackoverflow.com/a/11056415 – kabanus Apr 30 '18 at 10:29
  • 1
    Possible duplicate of [Python share values](https://stackoverflow.com/questions/32822013/python-share-values) – Yassine Faris Apr 30 '18 at 10:29
  • Variables are [not automatically shared](https://docs.python.org/3.6/library/multiprocessing.html#the-spawn-and-forkserver-start-methods) between processes. But you can use [shared memory](https://docs.python.org/3.6/library/multiprocessing.html#sharing-state-between-processes). – Klaus D. Apr 30 '18 at 10:31

0 Answers0