1# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4import multiprocessing
5import log
6import time
7
8
9_RealProcess = multiprocessing.Process
10__all__ = []
11
12
13class ProcessSubclass(_RealProcess):
14  def __init__(self, shim, *args, **kwards):
15    _RealProcess.__init__(self, *args, **kwards)
16    self._shim = shim
17
18  def run(self,*args,**kwargs):
19    log._disallow_tracing_control()
20    try:
21      r = _RealProcess.run(self, *args, **kwargs)
22    finally:
23      if log.trace_is_enabled():
24        log.trace_flush() # todo, reduce need for this...
25    return r
26
27class ProcessShim():
28  def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
29    self._proc = ProcessSubclass(self, group, target, name, args, kwargs)
30    # hint to testing code that the shimming worked
31    self._shimmed_by_trace_event = True
32
33  def run(self):
34    self._proc.run()
35
36  def start(self):
37    self._proc.start()
38
39  def terminate(self):
40    if log.trace_is_enabled():
41      # give the flush a chance to finish --> TODO: find some other way.
42      time.sleep(0.25)
43    self._proc.terminate()
44
45  def join(self, timeout=None):
46    self._proc.join( timeout)
47
48  def is_alive(self):
49    return self._proc.is_alive()
50
51  @property
52  def name(self):
53    return self._proc.name
54
55  @name.setter
56  def name(self, name):
57    self._proc.name = name
58
59  @property
60  def daemon(self):
61    return self._proc.daemon
62
63  @daemon.setter
64  def daemon(self, daemonic):
65    self._proc.daemon = daemonic
66
67  @property
68  def authkey(self):
69    return self._proc._authkey
70
71  @authkey.setter
72  def authkey(self, authkey):
73    self._proc.authkey = AuthenticationString(authkey)
74
75  @property
76  def exitcode(self):
77    return self._proc.exitcode
78
79  @property
80  def ident(self):
81    return self._proc.ident
82
83  @property
84  def pid(self):
85    return self._proc.pid
86
87  def __repr__(self):
88    return self._proc.__repr__()
89