富进度条与套接字文件共享不显示正确结果。

huangapple go评论71阅读模式
英文:

rich progress bar not showing correct results with sockets file sharing

问题

我有两个脚本 sender.pyreceiver.py。我从这里得到了计算接收到的块和发送的块的想法。

但我得到了一些不同的结果。进度条瞬间完成了100%,如这里所示。我做错了什么吗?

sender.py

import socket
from pip._vendor.rich.progress import (Progress, TextColumn, BarColumn, TransferSpeedColumn, DownloadColumn, TaskProgressColumn)

sock = socket.socket()
sock.connect(('127.0.0.1', 9999))
filename = "C:\\Users\\usename\\Downloads\\ubuntu-23.04-desktop-amd64.iso"
filesize = 4932407296
with Progress(TextColumn("[progress.description]{task.description}"), BarColumn(), TaskProgressColumn(), DownloadColumn(binary_units=True), TextColumn('at'), TransferSpeedColumn()) as progress:
    send_task = progress.add_task("Sending...", total=filesize)
    with open(filename, 'rb') as f:
        data_sent = 0
        while True:
            data = f.read(102400)
            if not data:
                break
            data_sent += sock.send(data)
            progress.update(send_task, advance=data_sent)
sock.close()

receiver.py

import socket
from pip._vendor.rich.progress import (Progress, TextColumn, BarColumn, TransferSpeedColumn, DownloadColumn, TaskProgressColumn)

sock = socket.socket()
sock.bind(("127.0.0.1", 9999))
sock.listen(10)  # 最多接受10个传入连接
conn, address = sock.accept()
filename = 'ubuntu-23.04-desktop-amd64.iso'
filesize = 4932407296
print(address)
with Progress(TextColumn("[progress.description]{task.description}"), BarColumn(), TaskProgressColumn(), DownloadColumn(binary_units=True), TextColumn('at'), TransferSpeedColumn()) as progress:
    recv_task = progress.add_task("Receiving...", total=filesize)
    with open(filename, 'wb') as f:
        data_received = 0
        while (data_received < filesize):
            data = conn.recv(102400)
            data_received += f.write(data)
            progress.update(recv_task, advance=data_received)
conn.close()
sock.close()
英文:

I have two scripts sender.py and reciever.py. i got the idea of calculating chunks recieved and chunks sent from here.

but i got some different results. the progress bar completed 100% instantly as shown here. am i doing something wrong?

sender.py

import socket
from pip._vendor.rich.progress import (Progress,TextColumn,BarColumn,TransferSpeedColumn, DownloadColumn, TaskProgressColumn)

sock = socket.socket()
sock.connect((&#39;127.0.0.1&#39;,9999))
filename = &quot;C:\\Users\\usename\\Downloads\\ubuntu-23.04-desktop-amd64.iso&quot;
filesize = 4932407296
with Progress(TextColumn(&quot;[progress.description]{task.description}&quot;), BarColumn(), TaskProgressColumn(), DownloadColumn(binary_units=True), TextColumn(&#39;at&#39;), TransferSpeedColumn()) as progress:
    send_task = progress.add_task(&quot;Sending...&quot;, total=filesize)
    with open(filename, &#39;rb&#39;) as f:
        data_sent = 0
        while True:
            data = f.read(102400)
            if not data:
                break
            data_sent += sock.send(data)
            progress.update(send_task, advance=data_sent)
sock.close()

reviever.py

import socket
from pip._vendor.rich.progress import (Progress,TextColumn,BarColumn,TransferSpeedColumn, DownloadColumn, TaskProgressColumn)

sock = socket.socket()
sock.bind((&quot;127.0.0.1&quot;,9999))
sock.listen(10) # Accepts up to 10 incoming connections..
conn, address = sock.accept()
filename = &#39;ubuntu-23.04-desktop-amd64.iso&#39;
filesize = 4932407296
print( address)
with Progress(TextColumn(&quot;[progress.description]{task.description}&quot;), BarColumn(), TaskProgressColumn(), DownloadColumn(binary_units=True), TextColumn(&#39;at&#39;), TransferSpeedColumn()) as progress:
    recv_task = progress.add_task(&quot;Recieving...&quot;, total=filesize)
    with open(filename,&#39;wb&#39;) as f:
        data_recieved = 0
        while (data_recieved &lt; filesize):
            data = conn.recv(102400)
            data_recieved += f.write(data)
            progress.update(recv_task, advance=data_recieved)
conn.close()
sock.close()

答案1

得分: 1

设置 advance增加已完成的值,增加的数量由指定的数值确定,而设置 completed 本身将覆盖它。

由于 data_sentdata_received 已经是总和,所以不需要使用以下代码:

progress.update(task, advance=amount)

你可能想要使用以下代码:

progress.update(task, completed=amount)

或者,你也可以完全不跟踪总数,只使用增量:

sender.py

import socket
from pip._vendor.rich.progress import (Progress, TextColumn, BarColumn, TransferSpeedColumn, DownloadColumn, TaskProgressColumn)

sock = socket.socket()
sock.connect(('127.0.0.1', 9999))
filename = "C:\\Users\\usename\\Downloads\\ubuntu-23.04-desktop-amd64.iso"
filesize = 4932407296
with Progress(TextColumn("[progress.description]{task.description}"), BarColumn(), TaskProgressColumn(), DownloadColumn(binary_units=True), TextColumn('at'), TransferSpeedColumn()) as progress:
    send_task = progress.add_task("Sending...", total=filesize)
    with open(filename, 'rb') as f:
        while True:
            data = f.read(102400)
            if not data:
                break
            delta = sock.send(data)
            progress.update(send_task, advance=delta)
sock.close()
英文:

Setting advance will add to the completed value by the specified ammount while setting completed itself will override it.

Since data_sent and data_received are already sums, instead of

progress.update(task, advance=amount)

You probably want

progress.update(task, completed=amount)

Altenatively, you could also avoid keeping track of the total entirely, and just use the delta:

(sender.py)

import socket
from pip._vendor.rich.progress import (Progress,TextColumn,BarColumn,TransferSpeedColumn, DownloadColumn, TaskProgressColumn)

sock = socket.socket()
sock.connect((&#39;127.0.0.1&#39;,9999))
filename = &quot;C:\\Users\\usename\\Downloads\\ubuntu-23.04-desktop-amd64.iso&quot;
filesize = 4932407296
with Progress(TextColumn(&quot;[progress.description]{task.description}&quot;), BarColumn(), TaskProgressColumn(), DownloadColumn(binary_units=True), TextColumn(&#39;at&#39;), TransferSpeedColumn()) as progress:
    send_task = progress.add_task(&quot;Sending...&quot;, total=filesize)
    with open(filename, &#39;rb&#39;) as f:
        while True:
            data = f.read(102400)
            if not data:
                break
            delta = sock.send(data)
            progress.update(send_task, advance=delta)
sock.close()

huangapple
  • 本文由 发表于 2023年6月12日 00:18:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定