How to get a working Progress Bar in python using multi-process or multi-threaded clients?
Asked Answered
H

1

6

That's the basic idea.

I couldn't understand them while reading about all this in python's documentation. There's simply too much of it to understand it.

Could someone explain me how to get a working progress bar with a multi-threaded(or multi-process) client?

Or is there any other way to "update" progress bar without locking program's GUI?

Also I did read something about "I/O" errors when these types of clients try to access a file at the same time & X-server errors when an application doesn't call multi-thread libs correctly. How do I avoid 'em?

This time I didn't write code, I don't want to end with a zombie-process or something like that (And then force PC to shutdown, I would hate to corrupt precious data...). I need to understand what am I doing first!

Hagiographer answered 21/8, 2016 at 5:56 Comment(2)
Hi, I recently acheived something similar (I think) using Tkinter, ttk and the threading module.... I had a moving indeterminate progress bar within my GUI window whilst I ran a function to query a databse through the threading module. The GUI does not lock. Does this sound comparable to your question?Istic
It sounds similar, but you're using Tkinter, while I'm trying to use PyGobject & Glade, however, it looks useful. Could you show me how to do it? Maybe some piece of code or something?Hagiographer
I
10

Something like the below might get you started? - Ive tried to annotate as much as possible to explain the process.

from Tkinter import *
from Queue import Queue
import ttk, threading
import time

queue = Queue()
root = Tk()

# Function to do 'stuff' and place object in queue for later #
def foo():
    # sleep to demonstrate thread doing work #
    time.sleep(5)
    obj = [x for x in range(0,10)]
    queue.put(obj)

# Create thread object, targeting function to do 'stuff' #
thread1 = threading.Thread(target=foo, args=())

# Function to check state of thread1 and to update progressbar #
def progress(thread, queue):
    # starts thread #
    thread.start()

    # defines indeterminate progress bar (used while thread is alive) #
    pb1 = ttk.Progressbar(root, orient='horizontal', mode='indeterminate')

    # defines determinate progress bar (used when thread is dead) #
    pb2 = ttk.Progressbar(root, orient='horizontal', mode='determinate')
    pb2['value'] = 100

    # places and starts progress bar #
    pb1.pack()
    pb1.start()

    # checks whether thread is alive #
    while thread.is_alive():
        root.update()
        pass

    # once thread is no longer active, remove pb1 and place the '100%' progress bar #
    pb1.destroy()
    pb2.pack()

    # retrieves object from queue #
    work = queue.get()
    return work

work = progress(thread1, queue)
root.mainloop()

Hope this helps. Let me know what you think!

Istic answered 25/8, 2016 at 10:20 Comment(5)
Yay! Finally some kind of help! Thanks. I've been busy lately doing some misc stuff. In fact, this is the first time I open SO in 3 days. Thanks, I'll try to modify your anwer to fit my proyect. THANKS! (You'ŕe the first one to answer SOMETHING (Not comment) in one of my threads)Hagiographer
Happy to help! :) Mark as best answer and upvote? :DIstic
I'm still tryting to do somethin' with this code. Don't worry. If I manage to achieve my purpose, I'll do itHagiographer
Nope :c Still trying by my own.Hagiographer
This example deserves some upvote love. Good guidance to tune for other use-cases.Hindquarter

© 2022 - 2024 — McMap. All rights reserved.