<commit_before>import time
import multiprocessing
import signal

from hokusai.command import command
from hokusai.config import config
from hokusai.common import shout
from hokusai.kubectl import Kubectl

def get_logs(kctl, pod_name, opts):
  shout(kctl.command("logs %s%s" % (pod_name, opts)), print_output=True)

@command
def logs(context, timestamps, nlines, follow):
  kctl = Kubectl(context)

  opts = ''
  if timestamps:
    opts += ' --timestamps'
  if nlines:
    opts += " --tail=%s" % nlines
  if follow:
    opts += ' --follow'

  pods = kctl.get_object('pod', selector="app=%s" % config.project_name)['items']

  if follow:
    # Get logs from each Pod async

    # Pool processes inherit signal handlers from the parent process, so to ignore it detatch the SIGINT handler, create the Pool, then re-attach
    # http://stackoverflow.com/questions/11312525/catch-ctrlc-sigint-and-exit-multiprocesses-gracefully-in-python
    original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
    pool = multiprocessing.Pool(len(pods))
    signal.signal(signal.SIGINT, original_sigint_handler)

    for pod in pods:
      pool.apply_async(get_logs, (kctl, pod['metadata']['name'], opts))

    try:
      while True:
        time.sleep(1)
    except KeyboardInterrupt:
      pool.terminate()

  else:
    # Get logs from each pod sync
    for pod in pods:
      get_logs(kctl, pod['metadata']['name'], opts)
<commit_msg>in logs command use Popen for subprocesses<commit_after>from subprocess import Popen

from hokusai.command import command
from hokusai.config import config
from hokusai.common import shout
from hokusai.kubectl import Kubectl

@command
def logs(context, timestamps, nlines, follow):
  kctl = Kubectl(context)

  opts = ''
  if timestamps:
    opts += ' --timestamps'
  if nlines:
    opts += " --tail=%s" % nlines
  if follow:
    opts += ' --follow'

  pods = kctl.get_object('pod', selector="app=%s" % config.project_name)['items']

  if follow:
    processes = [Popen(kctl.command("logs %s%s" % (pod['metadata']['name'], opts)), shell=True) for pod in pods]
    try:
      for p in processes:
        p.wait()
    except KeyboardInterrupt:
      for p in processes:
        p.terminate()
  else:
    for pod in pods:
      shout(kctl.command("logs %s%s" % (pod['metadata']['name'], opts)), print_output=True)

{'fraction_non_alphanumeric': 0.12286689419795221, 'fraction_numerical': 0.0038395904436860067, 'mean_word_length': 3.708835341365462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5968656', 'n_tokens_mistral': 803, 'n_tokens_neox': 780, 'n_words': 211}