@@ -333,6 +333,9 @@ def _ip_changed(self, name, old, new):
333
333
control_port = Integer (0 , config = True ,
334
334
help = "set the control (ROUTER) port [default: random]" )
335
335
336
+ # names of the ports with random assignment
337
+ _random_port_names = None
338
+
336
339
@property
337
340
def ports (self ):
338
341
return [ getattr (self , name ) for name in port_names ]
@@ -417,6 +420,22 @@ def cleanup_ipc_files(self):
417
420
except (IOError , OSError ):
418
421
pass
419
422
423
+ def _record_random_port_names (self ):
424
+ """Records which of the ports are randomly assigned.
425
+
426
+ Records on first invocation. Does nothing on later invocations."""
427
+
428
+ if self .transport != 'tcp' :
429
+ return
430
+ if self ._random_port_names is not None :
431
+ return
432
+
433
+ self ._random_port_names = []
434
+ for name in port_names :
435
+ if getattr (self , name ) <= 0 :
436
+ self ._random_port_names .append (name )
437
+
438
+
420
439
def write_connection_file (self ):
421
440
"""Write connection info to JSON dict in self.connection_file."""
422
441
if self ._connection_file_written and os .path .exists (self .connection_file ):
@@ -431,6 +450,7 @@ def write_connection_file(self):
431
450
kernel_name = self .kernel_name
432
451
)
433
452
# write_connection_file also sets default ports:
453
+ self ._record_random_port_names ()
434
454
for name in port_names :
435
455
setattr (self , name , cfg [name ])
436
456
@@ -467,6 +487,7 @@ def load_connection_info(self, info):
467
487
self .transport = info .get ('transport' , self .transport )
468
488
self .ip = info .get ('ip' , self ._ip_default ())
469
489
490
+ self ._record_random_port_names ()
470
491
for name in port_names :
471
492
if getattr (self , name ) == 0 and name in info :
472
493
# not overridden by config or cl_args
0 commit comments