aboutsummaryrefslogtreecommitdiff
path: root/catapult/common/py_trace_event/py_trace_event/trace_event_impl/multiprocessing_shim.py
blob: c2295edafa2cf51d2b42a7d1811d856a1e29924c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import multiprocessing
import log
import time


_RealProcess = multiprocessing.Process
__all__ = []


class ProcessSubclass(_RealProcess):
  def __init__(self, shim, *args, **kwards):
    _RealProcess.__init__(self, *args, **kwards)
    self._shim = shim

  def run(self,*args,**kwargs):
    log._disallow_tracing_control()
    try:
      r = _RealProcess.run(self, *args, **kwargs)
    finally:
      if log.trace_is_enabled():
        log.trace_flush() # todo, reduce need for this...
    return r

class ProcessShim():
  def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
    self._proc = ProcessSubclass(self, group, target, name, args, kwargs)
    # hint to testing code that the shimming worked
    self._shimmed_by_trace_event = True

  def run(self):
    self._proc.run()

  def start(self):
    self._proc.start()

  def terminate(self):
    if log.trace_is_enabled():
      # give the flush a chance to finish --> TODO: find some other way.
      time.sleep(0.25)
    self._proc.terminate()

  def join(self, timeout=None):
    self._proc.join( timeout)

  def is_alive(self):
    return self._proc.is_alive()

  @property
  def name(self):
    return self._proc.name

  @name.setter
  def name(self, name):
    self._proc.name = name

  @property
  def daemon(self):
    return self._proc.daemon

  @daemon.setter
  def daemon(self, daemonic):
    self._proc.daemon = daemonic

  @property
  def authkey(self):
    return self._proc._authkey

  @authkey.setter
  def authkey(self, authkey):
    self._proc.authkey = AuthenticationString(authkey)

  @property
  def exitcode(self):
    return self._proc.exitcode

  @property
  def ident(self):
    return self._proc.ident

  @property
  def pid(self):
    return self._proc.pid

  def __repr__(self):
    return self._proc.__repr__()