Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: b9f076b36f71a8728d5aafceaa8348be5e1aec19bf7e262893756fe38b35ca79
Ticket: 7ec0aa14009019ca1a58ed0021c8ca8f0f3f4f5f
http -progress callback invoked multiple times with no progress
User & Date: anonymous on 2024-06-01 19:59:26
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/plain"
  4. comment changed to:
    The progress callback is being called more times than would be expected, and called with apparently no progress. An added delay in the callback does change the behavior, however, and with a 300 ms delay added, the issue does not occur.
    
    It seems as though the callback is simply called every so often, regardless of the actual progress.
    
    The following test program creates a toplevel with a ttk::progressbar, a label, and a button to start an http transfer. The url is built into the program.
    
    It reports the 3 args sent into the -progress callback with a puts, and then to be efficient only updates the progress bar if the progress has changed by at least 1%
    
    Different values of -blocksize may or may not effect the results. 
    
    Here's the test code (works on both linux and windows):
    
    package require Tk
    package require http
    
    if { $::tcl_platform(platform) ne "windows"  } {
        package require tls
        http::register https 443 [list ::tls::socket -ssl2 0 -ssl3 0 -tls1 1 -tls1.1 1 -tls1.2 1 ]
    } else {
        console show
        package require twapi_crypto
        http::register https 443 [list ::twapi::tls_socket]
    }
    
    
    wm withdraw .
    
    namespace eval ProgressBar {
        variable progress_var 0
        variable max_progress_var 0
        variable increment_value 1
        variable percent_label ""
        variable last_percent -1  ;# Initialize to -1 to ensure initial update
        
        # Procedure to create and initialize the progress bar
        proc create {title max_value} {
            variable progress_var
            variable max_progress_var
            variable increment_value
            variable percent_label
            
            # Create the main window
            set win [toplevel .progress]
            wm title $win $title
            
            # Create a label
            label $win.label -text "Progress:"
            pack $win.label -padx 10 -pady 5
            
            # Initialize variables
            set progress_var 0
            set max_progress_var $max_value
            set increment_value 1
            
            # Create a progress bar widget
            ttk::progressbar $win.progress  -maximum 100 -length 300
            pack $win.progress -padx 10 -pady 10
            
            # Create a label to show the percentage done
            set percent_label [label $win.percent_label -text "0%"]
            pack $percent_label -padx 10 -pady 5
            
            # Button to start simulating fast HTTP events
            button $win.button -text "Start Fast Events" -command [namespace code ::ProgressBar::start_or_reset]
            pack $win.button -padx 10 -pady 5
            
        }
        
        # Procedure to start or reset the progress
        proc start_or_reset {} {
            variable progress_var
            variable max_progress_var
            puts start
            if {$progress_var >= $max_progress_var} {
                # Reset progress
                set progress_var 0
                .progress.button configure -text "Start Fast Events"
            }
            
            # Start the HTTP request with progress tracking
            start_http_request_with_progress_tracking
        }
        
        # Function to initialize progress tracking
        proc init_progress_tracking {expected_total} {
            global total_size segment_size
            set total_size $expected_total
            set segment_size [expr {$total_size / 100}]
        }
        
        #   Callback function to handle HTTP progress
        proc httpProgress {token total_size bytes_transferred} {
            global progress_var max_progress_var increment_value last_percent
            
            # Initialize last_percent if not already initialized
            if {! [info exists last_percent]} {
                set last_percent -1
            }
            
            # Initialize progress tracking if not already initialized
            if {! [info exists total_size]} {
                ProgressBar::init_progress_tracking $total_size
            }
            
            # Calculate the progress if the total size is available and not zero
            if {$total_size > 0} {
                set progress [expr {int(($bytes_transferred / double($total_size)) * 100)}]
            } else {
                # Handle the case where total size is not available
                set progress [expr {int(($bytes_transferred / double($max_progress_var)) * 100)}]
                
            }
            
            # Update the progress bar only when the percentage changes
            if {$progress != $last_percent} {
                puts "last_percent= |$last_percent| progress= |$progress| "
                set last_percent $progress
                
                ProgressBar::update_progress_bar $progress
    #           after 300 ;# manually added so I can see visually the progress bar change
            }
            
            # Increment the progress variable
            set progress_var [expr {$progress_var + $increment_value}]
            
            # Ensure progress_var does not exceed max_progress_var
            if {$progress_var >= $max_progress_var} {
                set progress_var $max_progress_var
                .progress.button configure -text "Done"
            }
        }
        
        #   Function to update the progress bar and label
        proc update_progress_bar {percent_done} {
            variable max_progress_var
            
            # Update the label with the current percentage done
            .progress.percent_label configure -text "$percent_done%"
            
            # Update the progress bar widget
            .progress.progress configure -value $percent_done
            update idletasks
        }
        
        #   Main procedure to start the HTTP request with progress tracking
        proc start_http_request_with_progress_tracking {} {
            global progress_var max_progress_var increment_value
            puts inside-start
            # Initialize progress tracking variables
            set progress_var 0
            set max_progress_var 0 ;# Set the maximum progress value
            set increment_value 100    ;# Increment value for each progress update
            
            # Start the HTTP request with progress callback
    #        set url "https://en.wikipedia.org/wiki/Main_Page"
            set url "https://tkdocs.com/tutorial/onepage.html"
            set token [::http::geturl $url  -progress [namespace code {httpProgressCallback}]]
            puts "token= |$token| "
            # Wait for the HTTP request to complete
            ::http::wait $token
            puts done
        }
        
        #   Callback function to handle HTTP progress
        proc httpProgressCallback {token total_size bytes_transferred} {
            puts "token= |$token| total_size= |$total_size| bytes_transferred= |$bytes_transferred| "
            
            # Call httpProgress with the provided arguments
            httpProgress $token $total_size $bytes_transferred
        }
        
    }
    
    
    #Main script to run the example
    ProgressBar::create "Progress Bar Example" 100
    
    #Start the Tk event loop
    tkwait window .progress
    
  5. foundin changed to: "8.6.13"
  6. is_private changed to: "0"
  7. login: "anonymous"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "6a1053dc0f0104e70826d300a9b9d4a6081b23c6"
  10. resolution changed to: "None"
  11. severity changed to: "Minor"
  12. status changed to: "Open"
  13. submitter changed to: "anonymous"
  14. subsystem changed to: "29. http Package"
  15. title changed to:
    http -progress callback invoked multiple times with no progress
    
  16. type changed to: "Bug"