home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / multiprocessing / process.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-11-11  |  9.3 KB  |  296 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'Process',
  6.     'current_process',
  7.     'active_children']
  8. import os
  9. import sys
  10. import signal
  11. import itertools
  12.  
  13. try:
  14.     ORIGINAL_DIR = os.path.abspath(os.getcwd())
  15. except OSError:
  16.     ORIGINAL_DIR = None
  17.  
  18.  
  19. def current_process():
  20.     '''
  21.     Return process object representing the current process
  22.     '''
  23.     return _current_process
  24.  
  25.  
  26. def active_children():
  27.     '''
  28.     Return list of process objects corresponding to live child processes
  29.     '''
  30.     _cleanup()
  31.     return list(_current_process._children)
  32.  
  33.  
  34. def _cleanup():
  35.     for p in list(_current_process._children):
  36.         if p._popen.poll() is not None:
  37.             _current_process._children.discard(p)
  38.             continue
  39.     
  40.  
  41.  
  42. class Process(object):
  43.     '''
  44.     Process objects represent activity that is run in a separate process
  45.  
  46.     The class is analagous to `threading.Thread`
  47.     '''
  48.     _Popen = None
  49.     
  50.     def __init__(self, group = None, target = None, name = None, args = (), kwargs = { }):
  51.         if not group is None:
  52.             raise AssertionError, 'group argument must be None for now'
  53.         count = _current_process._counter.next()
  54.         self._identity = _current_process._identity + (count,)
  55.         self._authkey = _current_process._authkey
  56.         self._daemonic = _current_process._daemonic
  57.         self._tempdir = _current_process._tempdir
  58.         self._parent_pid = os.getpid()
  59.         self._popen = None
  60.         self._target = target
  61.         self._args = tuple(args)
  62.         self._kwargs = dict(kwargs)
  63.         if not name:
  64.             pass
  65.         self._name = type(self).__name__ + '-' + ':'.join((lambda .0: for i in .0:
  66. str(i))(self._identity))
  67.  
  68.     
  69.     def run(self):
  70.         '''
  71.         Method to be run in sub-process; can be overridden in sub-class
  72.         '''
  73.         if self._target:
  74.             self._target(*self._args, **self._kwargs)
  75.         
  76.  
  77.     
  78.     def start(self):
  79.         '''
  80.         Start child process
  81.         '''
  82.         if not self._popen is None:
  83.             raise AssertionError, 'cannot start a process twice'
  84.         if not self._parent_pid == os.getpid():
  85.             raise AssertionError, 'can only start a process object created by current process'
  86.         if not not (_current_process._daemonic):
  87.             raise AssertionError, 'daemonic processes are not allowed to have children'
  88.         _cleanup()
  89.         self._popen = Popen(self)
  90.         _current_process._children.add(self)
  91.  
  92.     
  93.     def terminate(self):
  94.         '''
  95.         Terminate process; sends SIGTERM signal or uses TerminateProcess()
  96.         '''
  97.         self._popen.terminate()
  98.  
  99.     
  100.     def join(self, timeout = None):
  101.         '''
  102.         Wait until child process terminates
  103.         '''
  104.         if not self._parent_pid == os.getpid():
  105.             raise AssertionError, 'can only join a child process'
  106.         if not self._popen is not None:
  107.             raise AssertionError, 'can only join a started process'
  108.         res = self._popen.wait(timeout)
  109.  
  110.     
  111.     def is_alive(self):
  112.         '''
  113.         Return whether process is alive
  114.         '''
  115.         if self is _current_process:
  116.             return True
  117.         if not self._parent_pid == os.getpid():
  118.             raise AssertionError, 'can only test a child process'
  119.         if self._popen is None:
  120.             return False
  121.         self._popen.poll()
  122.         return self._popen.returncode is None
  123.  
  124.     
  125.     def name(self):
  126.         return self._name
  127.  
  128.     name = property(name)
  129.     
  130.     def name(self, name):
  131.         if not isinstance(name, str):
  132.             raise AssertionError, 'name must be a string'
  133.         self._name = name
  134.  
  135.     name = name.setter(name)
  136.     
  137.     def daemon(self):
  138.         '''
  139.         Return whether process is a daemon
  140.         '''
  141.         return self._daemonic
  142.  
  143.     daemon = property(daemon)
  144.     
  145.     def daemon(self, daemonic):
  146.         '''
  147.         Set whether process is a daemon
  148.         '''
  149.         if not self._popen is None:
  150.             raise AssertionError, 'process has already started'
  151.         self._daemonic = daemonic
  152.  
  153.     daemon = daemon.setter(daemon)
  154.     
  155.     def authkey(self):
  156.         return self._authkey
  157.  
  158.     authkey = property(authkey)
  159.     
  160.     def authkey(self, authkey):
  161.         '''
  162.         Set authorization key of process
  163.         '''
  164.         self._authkey = AuthenticationString(authkey)
  165.  
  166.     authkey = authkey.setter(authkey)
  167.     
  168.     def exitcode(self):
  169.         '''
  170.         Return exit code of process or `None` if it has yet to stop
  171.         '''
  172.         if self._popen is None:
  173.             return self._popen
  174.         return self._popen.poll()
  175.  
  176.     exitcode = property(exitcode)
  177.     
  178.     def ident(self):
  179.         '''
  180.         Return indentifier (PID) of process or `None` if it has yet to start
  181.         '''
  182.         if self is _current_process:
  183.             return os.getpid()
  184.         if self._popen:
  185.             pass
  186.         return self._popen.pid
  187.  
  188.     ident = property(ident)
  189.     pid = ident
  190.     
  191.     def __repr__(self):
  192.         if self is _current_process:
  193.             status = 'started'
  194.         elif self._parent_pid != os.getpid():
  195.             status = 'unknown'
  196.         elif self._popen is None:
  197.             status = 'initial'
  198.         elif self._popen.poll() is not None:
  199.             status = self.exitcode
  200.         else:
  201.             status = 'started'
  202.         if type(status) is int:
  203.             if status == 0:
  204.                 status = 'stopped'
  205.             else:
  206.                 status = 'stopped[%s]' % _exitcode_to_name.get(status, status)
  207.         
  208.         if not self._daemonic or ' daemon':
  209.             pass
  210.         return '<%s(%s, %s%s)>' % (type(self).__name__, self._name, status, '')
  211.  
  212.     
  213.     def _bootstrap(self):
  214.         global _current_process
  215.         util = util
  216.         import 
  217.         
  218.         try:
  219.             self._children = set()
  220.             self._counter = itertools.count(1)
  221.             
  222.             try:
  223.                 sys.stdin.close()
  224.                 sys.stdin = open(os.devnull)
  225.             except (OSError, ValueError):
  226.                 pass
  227.  
  228.             _current_process = self
  229.             util._finalizer_registry.clear()
  230.             util._run_after_forkers()
  231.             util.info('child process calling self.run()')
  232.             
  233.             try:
  234.                 self.run()
  235.                 exitcode = 0
  236.             finally:
  237.                 util._exit_function()
  238.  
  239.         except SystemExit:
  240.             e = None
  241.             if not e.args:
  242.                 exitcode = 1
  243.             elif type(e.args[0]) is int:
  244.                 exitcode = e.args[0]
  245.             else:
  246.                 sys.stderr.write(e.args[0] + '\n')
  247.                 sys.stderr.flush()
  248.                 exitcode = 1
  249.         except:
  250.             e.args
  251.             exitcode = 1
  252.             import traceback
  253.             sys.stderr.write('Process %s:\n' % self.name)
  254.             sys.stderr.flush()
  255.             traceback.print_exc()
  256.  
  257.         util.info('process exiting with exitcode %d' % exitcode)
  258.         return exitcode
  259.  
  260.  
  261.  
  262. class AuthenticationString(bytes):
  263.     
  264.     def __reduce__(self):
  265.         Popen = Popen
  266.         import forking
  267.         if not Popen.thread_is_spawning():
  268.             raise TypeError('Pickling an AuthenticationString object is disallowed for security reasons')
  269.         Popen.thread_is_spawning()
  270.         return (AuthenticationString, (bytes(self),))
  271.  
  272.  
  273.  
  274. class _MainProcess(Process):
  275.     
  276.     def __init__(self):
  277.         self._identity = ()
  278.         self._daemonic = False
  279.         self._name = 'MainProcess'
  280.         self._parent_pid = None
  281.         self._popen = None
  282.         self._counter = itertools.count(1)
  283.         self._children = set()
  284.         self._authkey = AuthenticationString(os.urandom(32))
  285.         self._tempdir = None
  286.  
  287.  
  288. _current_process = _MainProcess()
  289. del _MainProcess
  290. _exitcode_to_name = { }
  291. for name, signum in signal.__dict__.items():
  292.     if name[:3] == 'SIG' and '_' not in name:
  293.         _exitcode_to_name[-signum] = name
  294.         continue
  295.  
  296.