@@ -435,70 +435,70 @@ def run_bandwidth_client(**kwargs):
435435def run_bandwidth_test (func , args , nthreads ):
436436 # Create a listening socket to receive the packets. We use UDP which should
437437 # be painlessly cross-platform.
438- sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
439- sock .bind (("127.0.0.1" , 0 ))
440- addr = sock .getsockname ()
438+ with socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) as sock :
439+ sock .bind (("127.0.0.1" , 0 ))
440+ addr = sock .getsockname ()
441441
442- duration = BANDWIDTH_DURATION
443- packet_size = BANDWIDTH_PACKET_SIZE
444-
445- results = []
446- threads = []
447- end_event = []
448- start_cond = threading .Condition ()
449- started = False
450- if nthreads > 0 :
451- # Warm up
452- func (* args )
442+ duration = BANDWIDTH_DURATION
443+ packet_size = BANDWIDTH_PACKET_SIZE
453444
454445 results = []
455- loop = TimedLoop (func , args )
456- ready = []
457- ready_cond = threading .Condition ()
458-
459- def run ():
446+ threads = []
447+ end_event = []
448+ start_cond = threading .Condition ()
449+ started = False
450+ if nthreads > 0 :
451+ # Warm up
452+ func (* args )
453+
454+ results = []
455+ loop = TimedLoop (func , args )
456+ ready = []
457+ ready_cond = threading .Condition ()
458+
459+ def run ():
460+ with ready_cond :
461+ ready .append (None )
462+ ready_cond .notify ()
463+ with start_cond :
464+ while not started :
465+ start_cond .wait ()
466+ loop (start_time , duration * 1.5 , end_event , do_yield = False )
467+
468+ for i in range (nthreads ):
469+ threads .append (threading .Thread (target = run ))
470+ for t in threads :
471+ t .setDaemon (True )
472+ t .start ()
473+ # Wait for threads to be ready
460474 with ready_cond :
461- ready .append (None )
462- ready_cond .notify ()
463- with start_cond :
464- while not started :
465- start_cond .wait ()
466- loop (start_time , duration * 1.5 , end_event , do_yield = False )
467-
468- for i in range (nthreads ):
469- threads .append (threading .Thread (target = run ))
470- for t in threads :
471- t .setDaemon (True )
472- t .start ()
473- # Wait for threads to be ready
474- with ready_cond :
475- while len (ready ) < nthreads :
476- ready_cond .wait ()
477-
478- # Run the client and wait for the first packet to arrive before
479- # unblocking the background threads.
480- process = run_bandwidth_client (addr = addr ,
481- packet_size = packet_size ,
482- duration = duration )
483- _time = time .time
484- # This will also wait for the parent to be ready
485- s = _recv (sock , packet_size )
486- remote_addr = eval (s .partition ('#' )[0 ])
487-
488- with start_cond :
489- start_time = _time ()
490- started = True
491- start_cond .notify (nthreads )
492-
493- n = 0
494- first_time = None
495- while not end_event and BW_END not in s :
496- _sendto (sock , s , remote_addr )
475+ while len (ready ) < nthreads :
476+ ready_cond .wait ()
477+
478+ # Run the client and wait for the first packet to arrive before
479+ # unblocking the background threads.
480+ process = run_bandwidth_client (addr = addr ,
481+ packet_size = packet_size ,
482+ duration = duration )
483+ _time = time .time
484+ # This will also wait for the parent to be ready
497485 s = _recv (sock , packet_size )
498- if first_time is None :
499- first_time = _time ()
500- n += 1
501- end_time = _time ()
486+ remote_addr = eval (s .partition ('#' )[0 ])
487+
488+ with start_cond :
489+ start_time = _time ()
490+ started = True
491+ start_cond .notify (nthreads )
492+
493+ n = 0
494+ first_time = None
495+ while not end_event and BW_END not in s :
496+ _sendto (sock , s , remote_addr )
497+ s = _recv (sock , packet_size )
498+ if first_time is None :
499+ first_time = _time ()
500+ n += 1
501+ end_time = _time ()
502502
503503 end_event .append (None )
504504 for t in threads :
0 commit comments