Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bugfixes in example due to threading

  • Loading branch information...
commit 2c4b3ecc2e5a787c298b46b8d77f8b4bb7d9f9dd 1 parent 779df8b
@deavid authored
Showing with 39 additions and 15 deletions.
  1. +1 −0  bjsonrpc/filetransfer/__init__.py
  2. +38 −15 examples/example-filetransfer.py
View
1  bjsonrpc/filetransfer/__init__.py
@@ -60,6 +60,7 @@ def write(self, data):
data = data.data
elif self.decode:
data = self.decode(data)
+
return self.fileobj.write(data)
def read(self, size, donotsend = False):
View
53 examples/example-filetransfer.py
@@ -64,7 +64,7 @@
class MyHandler(BaseHandler):
def newWriteFileTransfer(self):
#fobj = StringIO()
- fobj = open(DST_FILE,"w")
+ fobj = io.FileIO(DST_FILE,"w") # instead of open, because is a thread-safe version
return FileTransferHandler(self,fobj,'w')
def echo(self, string):
@@ -79,7 +79,7 @@ def stop(self):
sys.exit(0)
import threading, os, os.path
-import hashlib, traceback
+import hashlib, traceback, io
def sha1digest(filename):
hashobj = hashlib.sha1()
@@ -88,22 +88,26 @@ def sha1digest(filename):
x = f1.read(256*1024)
if not x: break
hashobj.update(x)
+ pos = f1.tell()
f1.close()
- return hashobj.hexdigest()
+ return hashobj.hexdigest(), pos
def thread1():
- time.sleep(0.1)
+ time.sleep(0.5)
print "Connecting to server at %s:%s" % (RPC_HOST, RPC_PORT)
conn = bjsonrpc.connect(host=RPC_HOST,port=RPC_PORT)
try:
conn.call.echo("Connected!")
filename = os.path.realpath(SEND_FILE)
- fsz = os.path.getsize(filename)
+ #fsz = os.path.getsize(filename)
+ #statinfo = os.stat(filename)
+ #fsz = statinfo.st_size
+ sha1_orig, fsz = sha1digest(filename)
print "File: %s Size: %.3fMb" % (filename, fsz/1024.0/1024.0)
- sha1_orig = sha1digest(filename)
print "Source File SHA-1 Digest:", sha1_orig
- fread = open(filename,"r")
+
+ fread = io.FileIO(filename,"r") # instead of open, because is a thread-safe version
@@ -111,33 +115,52 @@ def thread1():
t1 = tstart = time.time()
# Thread version of:
# ... fth.send_file(fread)
- subth1 = threading.Thread(target=fth.send_file, args=(fread,))
+ def send_file(fth, fread):
+ try:
+ fth.send_file(fread)
+ except:
+ print traceback.format_exc()
+ subth1 = threading.Thread(target=send_file, args=(fth,fread))
subth1.daemon = False
subth1.start()
-
- while subth1.isAlive():
+ alive = True
+ while alive:
try:
- subth1.join(0.1)
+ subth1.join(0.01)
except Exception, e:
print e
break
+ alive = subth1.isAlive()
t1 = time.time()
delta = t1 - tstart
+ lpos = pos = fread.tell()
pos = fth.tell()
+ dpos = lpos - pos
+ if dpos != 0:
+ print "Delta!!", dpos
posmb = pos / 1024.0 / 1024.0
- ppos = pos * 100.0 / float(fsz)
+ ppos = (pos+1) / float(fsz) * 100.0
rem = delta / ppos * (100 - ppos)
speedmb = posmb / delta
- status = ("working, %.2fs remaining" % rem if subth1.isAlive() else "done after %.2f seconds!" % delta)
+ status = ("working, %.2fs remaining" % rem if alive else "done after %.2f seconds!" % delta)
sys.stdout.write("Pos %.1f%%: %.3f MB @ %.1fMB/s -- %s%s\r" % (ppos,posmb,speedmb, status ," "*10))
sys.stdout.flush()
print
- del fth
- sha1_dst = sha1digest(DST_FILE)
+ if fsz != fread.tell():
+ print "Source Data: Differences between size and final position:", fsz , fread.tell()
+ data_remaining = fread.read(100)
+ if data_remaining:
+ raise AssertionError
+ sha1_dst, dsz = sha1digest(DST_FILE)
+ if dsz != fth.tell():
+ print "Target Data: Differences between size and final position:", dsz , fth.tell()
print "Recv. File SHA-1 Digest:", sha1_dst
+ if fsz != dsz:
+ print "Byte-count differ in %d bytes:", fsz- dsz
assert(sha1_dst == sha1_orig)
finally:
+ del fth
conn.notify.stop()
conn.close()
Please sign in to comment.
Something went wrong with that request. Please try again.