0

I'm trying to gather some metrics regarding a network issue in a small LAN with SSH disconnecting occasionally and ping exhibiting huge delays (up to a minute instead of less than a second!).

Using timeit (I've read that it's a good way to check elapsed execution time when calling some code snippet) I try to download some data from a FTP server that also runs locally, measure the time and store it inside a log file.

from ftplib import FTP
from timeit import timeit
from datetime import datetime

ftp = FTP(host='10.0.0.8')
ftp.login(user='****', passw='****')
ftp.cwd('updates/')
ftp.retrlines('LIST')

# Get timestamp when the download starts
curr_t = datetime.now()

print('Download large file and measure the time')
file_big_t = timeit(lambda f=ftp: f.retrbinary('RETR update_big', open('/tmp/update_big', 'wb').write))

print('Download medium file and measure the time')
file_medium_t = timeit(lambda f=ftp: f.retrbinary('RETR file_medium ', open('/tmp/file_medium ', 'wb').write))

print('Download small file and measure the time')
file_small_t = timeit(lambda f=ftp: f.retrbinary('RETR update_small', open('/tmp/update_small', 'wb').write))

# Write timestamp and measured timings to file
# ...

If I call the retrbinary(...) without the timeit it works without any issues. However the code above results in the script freezing right after the first timeit call.

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161

1 Answers1

0

In case someone else wants to do what I've described in my question I found the solution here. For some reason passing the lambda directly to timeit results in the behaviour I've already mentioned. However if the same lambda is first passed to an instance of timeit.Timer and that that instance's timeit() function is called it works.

For the example above (let's take just file_big_t) I did

file_big_timer = Timer(lambda f=ftp: f.retrbinary('RETR update_big', open('/tmp/update_big', 'wb').write))
file_big_t = file_big_timer.timeit()
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161