英文:
rich progress bar not showing correct results with sockets file sharing
问题
我有两个脚本 sender.py
和 receiver.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(('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()
reviever.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) # Accepts up to 10 incoming connections..
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("Recieving...", total=filesize)
with open(filename,'wb') as f:
data_recieved = 0
while (data_recieved < 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_sent
和 data_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(('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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论