github.com/annchain/OG@v0.0.9/scripts/p2p_sim/run.py (about)

     1  import os
     2  import select
     3  import subprocess
     4  import sys
     5  from threading import Thread
     6  from typing import List, Any
     7  
     8  import config_generator
     9  
    10  binary = '../build/og'
    11  params = '-c configs/config_XX.toml -d data/d_XX -l data/datadir_XX -n -M  run'
    12  
    13  current_node_id = 0
    14  pids: List[subprocess.Popen] = []
    15  
    16  
    17  def keep_read(process, i):
    18      while True:
    19          output = process.stdout.readline()
    20          if output == '' and process.poll() is not None:
    21              print('Quiting', i)
    22              break
    23          if output and b'[GIN-debug]' not in output:
    24              print(i, output.decode('utf-8').strip())
    25      rc = process.poll()
    26  
    27  
    28  def add_node():
    29      global current_node_id
    30      global pids
    31  
    32      i = current_node_id
    33      #config_generate.generate_config(i, i == 0)
    34  
    35      p = params.replace('XX', '%02d' % (i))
    36      print(binary + " " + p)
    37      pp = p.split(' ')
    38      pp.insert(0, binary)
    39  
    40      pid = subprocess.Popen(pp, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    41      pids.append(pid)
    42      print('Started %d' % current_node_id)
    43      current_node_id += 1
    44  
    45      t = Thread(target=keep_read, args=[pid, i],daemon=True)
    46      t.start()
    47  
    48  
    49  def del_node():
    50      global current_node_id
    51      global pids
    52  
    53      current_node_id -= 1
    54      pids[current_node_id].terminate()
    55      pids[current_node_id].wait()
    56      pids = pids[:-1]
    57      print('Terminated %d' % current_node_id)
    58  
    59  
    60  if __name__ == '__main__':
    61      for i in range(100):
    62          add_node()
    63  
    64      try:
    65          while True:
    66              inp, outp, err = select.select([sys.stdin], [], [])
    67              c = sys.stdin.read(1)
    68              if c == 'q':
    69                  while current_node_id > 0:
    70                      del_node()
    71              if c == '=':
    72                  for j in range(10):
    73                      add_node()
    74              if c == '-':
    75                  del_node()
    76      except KeyboardInterrupt as e:
    77          os.system("killall -v og -9")
    78          # os.system("killall -v og")