Python os.getpid() Examples
The following are 30 code examples for showing how to use os.getpid(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
os
, or try the search function
.
Example 1
Project: incubator-spot Author: apache File: collector.py License: Apache License 2.0 | 6 votes |
def ingest_file(file,message_size,topic,kafka_servers): logger = logging.getLogger('SPOT.INGEST.PROXY.{0}'.format(os.getpid())) try: message = "" logger.info("Ingesting file: {0} process:{1}".format(file,os.getpid())) with open(file,"rb") as f: for line in f: message += line if len(message) > message_size: KafkaProducer.SendMessage(message, kafka_servers, topic, 0) message = "" #send the last package. KafkaProducer.SendMessage(message, kafka_servers, topic, 0) rm_file = "rm {0}".format(file) Util.execute_cmd(rm_file,logger) logger.info("File {0} has been successfully sent to Kafka Topic: {1}".format(file,topic)) except Exception as err: logger.error("There was a problem, please check the following error message:{0}".format(err.message)) logger.error("Exception: {0}".format(err))
Example 2
Project: wechat-alfred-workflow Author: TKkk-iOSer File: util.py License: MIT License | 6 votes |
def atomic_writer(fpath, mode): """Atomic file writer. .. versionadded:: 1.12 Context manager that ensures the file is only written if the write succeeds. The data is first written to a temporary file. :param fpath: path of file to write to. :type fpath: ``unicode`` :param mode: sames as for :func:`open` :type mode: string """ suffix = '.{}.tmp'.format(os.getpid()) temppath = fpath + suffix with open(temppath, mode) as fp: try: yield fp os.rename(temppath, fpath) finally: try: os.remove(temppath) except (OSError, IOError): pass
Example 3
Project: cherrypy Author: cherrypy File: plugins.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, bus): self.bus = bus # Set default handlers self.handlers = {'SIGTERM': self.bus.exit, 'SIGHUP': self.handle_SIGHUP, 'SIGUSR1': self.bus.graceful, } if sys.platform[:4] == 'java': del self.handlers['SIGUSR1'] self.handlers['SIGUSR2'] = self.bus.graceful self.bus.log('SIGUSR1 cannot be set on the JVM platform. ' 'Using SIGUSR2 instead.') self.handlers['SIGINT'] = self._jython_SIGINT_handler self._previous_handlers = {} # used to determine is the process is a daemon in `self._is_daemonized` self._original_pid = os.getpid()
Example 4
Project: cherrypy Author: cherrypy File: plugins.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def _is_daemonized(self): """Return boolean indicating if the current process is running as a daemon. The criteria to determine the `daemon` condition is to verify if the current pid is not the same as the one that got used on the initial construction of the plugin *and* the stdin is not connected to a terminal. The sole validation of the tty is not enough when the plugin is executing inside other process like in a CI tool (Buildbot, Jenkins). """ return ( self._original_pid != os.getpid() and not os.isatty(sys.stdin.fileno()) )
Example 5
Project: fullrmc Author: bachiraoun File: Collection.py License: GNU Affero General Public License v3.0 | 6 votes |
def get_memory_usage(): """ Get current process memory usage. This is method requires psutils to be installed. :Returns: #. memory (float, None): The memory usage in Megabytes. When psutils is not installed, None is returned. """ try: import psutil process = psutil.Process(os.getpid()) memory = float( process.memory_info()[0] ) / float(2 ** 20) except: LOGGER.warn("memory usage cannot be profiled. psutil is not installed. pip install psutil") memory = None return memory
Example 6
Project: InsightAgent Author: insightfinder File: getmetrics_zipkin.py License: Apache License 2.0 | 6 votes |
def start_data_processing(): """ get traces from the last <samplingInterval> minutes """ # fork off a process to handle metric agent parent = os.fork() if parent > 0: track['mode'] = "LOG" if_config_vars['projectName'] += '-log' logger.debug(str(os.getpid()) + ' is running the log agent') else: track['mode'] = "METRIC" if_config_vars['projectName'] += '-metric' logger.debug(str(os.getpid()) + ' is running the metric agent') timestamp_fixed = int(time.time() * 1000) traces = zipkin_get_traces() for trace in traces: for span in trace: process_zipkin_span(timestamp_fixed, span)
Example 7
Project: rtp_cluster Author: sippy File: Rtp_proxy_client_udp.py License: BSD 2-Clause "Simplified" License | 6 votes |
def run(self): import os global_config = {} global_config['my_pid'] = os.getpid() rtpc = Rtp_proxy_client_udp(global_config, ('127.0.0.1', 22226), None) rtpc.rtpp_class = Rtp_proxy_client_udp os.system('sockstat | grep -w %d' % global_config['my_pid']) rtpc.send_command('Ib', self.gotreply) ED2.loop() rtpc.reconnect(('localhost', 22226), ('0.0.0.0', 34222)) os.system('sockstat | grep -w %d' % global_config['my_pid']) rtpc.send_command('V', self.gotreply) ED2.loop() rtpc.reconnect(('localhost', 22226), ('127.0.0.1', 57535)) os.system('sockstat | grep -w %d' % global_config['my_pid']) rtpc.send_command('V', self.gotreply) ED2.loop() rtpc.shutdown()
Example 8
Project: pointnet-registration-framework Author: vinits5 File: test_pointlk.py License: MIT License | 6 votes |
def run(args, testset, action): if not torch.cuda.is_available(): args.device = 'cpu' args.device = torch.device(args.device) LOGGER.debug('Testing (PID=%d), %s', os.getpid(), args) model = action.create_model() if args.pretrained: assert os.path.isfile(args.pretrained) model.load_state_dict(torch.load(args.pretrained, map_location='cpu')) model.to(args.device) # dataloader testloader = torch.utils.data.DataLoader( testset, batch_size=1, shuffle=False, num_workers=args.workers) # testing LOGGER.debug('tests, begin') action.eval_1(model, testloader, args.device) LOGGER.debug('tests, end')
Example 9
Project: certidude Author: laurivosandi File: common.py License: MIT License | 6 votes |
def drop_privileges(): from certidude import config import pwd _, _, uid, gid, gecos, root, shell = pwd.getpwnam("certidude") restricted_groups = [] restricted_groups.append(gid) # PAM needs access to /etc/shadow if config.AUTHENTICATION_BACKENDS == {"pam"}: import grp name, passwd, num, mem = grp.getgrnam("shadow") click.echo("Adding current user to shadow group due to PAM authentication backend") restricted_groups.append(num) os.setgroups(restricted_groups) os.setgid(gid) os.setuid(uid) click.echo("Switched %s (pid=%d) to user %s (uid=%d, gid=%d); member of groups %s" % (getproctitle(), os.getpid(), "certidude", os.getuid(), os.getgid(), ", ".join([str(j) for j in os.getgroups()]))) os.umask(0o007)
Example 10
Project: sanic Author: huge-success File: test_reloader.py License: MIT License | 6 votes |
def write_app(filename, **runargs): text = secrets.token_urlsafe() with open(filename, "w") as f: f.write( dedent( f"""\ import os from sanic import Sanic app = Sanic(__name__) @app.listener("after_server_start") def complete(*args): print("complete", os.getpid(), {text!r}) if __name__ == "__main__": app.run(**{runargs!r}) """ ) ) return text
Example 11
Project: sanic Author: huge-success File: worker.py License: MIT License | 6 votes |
def _check_alive(self): # If our parent changed then we shut down. pid = os.getpid() try: while self.alive: self.notify() req_count = sum( self.servers[srv]["requests_count"] for srv in self.servers ) if self.max_requests and req_count > self.max_requests: self.alive = False self.log.info( "Max requests exceeded, shutting down: %s", self ) elif pid == os.getpid() and self.ppid != os.getppid(): self.alive = False self.log.info("Parent changed, shutting down: %s", self) else: await asyncio.sleep(1.0, loop=self.loop) except (Exception, BaseException, GeneratorExit, KeyboardInterrupt): pass
Example 12
Project: LanczosNetwork Author: lrjconan File: arg_helper.py License: MIT License | 6 votes |
def get_config(config_file, exp_dir=None): """ Construct and snapshot hyper parameters """ config = edict(yaml.load(open(config_file, 'r'))) # create hyper parameters config.run_id = str(os.getpid()) config.exp_name = '_'.join([ config.model.name, config.dataset.name, time.strftime('%Y-%b-%d-%H-%M-%S'), config.run_id ]) if exp_dir is not None: config.exp_dir = exp_dir config.save_dir = os.path.join(config.exp_dir, config.exp_name) # snapshot hyperparameters mkdir(config.exp_dir) mkdir(config.save_dir) save_name = os.path.join(config.save_dir, 'config.yaml') yaml.dump(edict2dict(config), open(save_name, 'w'), default_flow_style=False) return config
Example 13
Project: jawfish Author: war-and-code File: test_break.py License: MIT License | 6 votes |
def testInterruptCaught(self): default_handler = signal.getsignal(signal.SIGINT) result = unittest.TestResult() unittest.installHandler() unittest.registerResult(result) self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler) def test(result): pid = os.getpid() os.kill(pid, signal.SIGINT) result.breakCaught = True self.assertTrue(result.shouldStop) try: test(result) except KeyboardInterrupt: self.fail("KeyboardInterrupt not handled") self.assertTrue(result.breakCaught)
Example 14
Project: jawfish Author: war-and-code File: test_break.py License: MIT License | 6 votes |
def testSecondInterrupt(self): result = unittest.TestResult() unittest.installHandler() unittest.registerResult(result) def test(result): pid = os.getpid() os.kill(pid, signal.SIGINT) result.breakCaught = True self.assertTrue(result.shouldStop) os.kill(pid, signal.SIGINT) self.fail("Second KeyboardInterrupt not raised") try: test(result) except KeyboardInterrupt: pass else: self.fail("Second KeyboardInterrupt not raised") self.assertTrue(result.breakCaught)
Example 15
Project: jawfish Author: war-and-code File: test_break.py License: MIT License | 6 votes |
def testTwoResults(self): unittest.installHandler() result = unittest.TestResult() unittest.registerResult(result) new_handler = signal.getsignal(signal.SIGINT) result2 = unittest.TestResult() unittest.registerResult(result2) self.assertEqual(signal.getsignal(signal.SIGINT), new_handler) result3 = unittest.TestResult() def test(result): pid = os.getpid() os.kill(pid, signal.SIGINT) try: test(result) except KeyboardInterrupt: self.fail("KeyboardInterrupt not handled") self.assertTrue(result.shouldStop) self.assertTrue(result2.shouldStop) self.assertFalse(result3.shouldStop)
Example 16
Project: jawfish Author: war-and-code File: test_break.py License: MIT License | 6 votes |
def testRemoveResult(self): result = unittest.TestResult() unittest.registerResult(result) unittest.installHandler() self.assertTrue(unittest.removeResult(result)) # Should this raise an error instead? self.assertFalse(unittest.removeResult(unittest.TestResult())) try: pid = os.getpid() os.kill(pid, signal.SIGINT) except KeyboardInterrupt: pass self.assertFalse(result.shouldStop)
Example 17
Project: jawfish Author: war-and-code File: regrtest.py License: MIT License | 6 votes |
def _make_temp_dir_for_build(TEMPDIR): # When tests are run from the Python build directory, it is best practice # to keep the test files in a subfolder. It eases the cleanup of leftover # files using command "make distclean". if sysconfig.is_python_build(): TEMPDIR = os.path.join(sysconfig.get_config_var('srcdir'), 'build') TEMPDIR = os.path.abspath(TEMPDIR) try: os.mkdir(TEMPDIR) except FileExistsError: pass # Define a writable temp dir that will be used as cwd while running # the tests. The name of the dir includes the pid to allow parallel # testing (see the -j option). TESTCWD = 'test_python_{}'.format(os.getpid()) TESTCWD = os.path.join(TEMPDIR, TESTCWD) return TEMPDIR, TESTCWD
Example 18
Project: jawfish Author: war-and-code File: util.py License: MIT License | 6 votes |
def __call__(self, wr=None, # Need to bind these locally because the globals can have # been cleared at shutdown _finalizer_registry=_finalizer_registry, sub_debug=sub_debug, getpid=os.getpid): ''' Run the callback unless it has already been called or cancelled ''' try: del _finalizer_registry[self._key] except KeyError: sub_debug('finalizer no longer registered') else: if self._pid != getpid(): sub_debug('finalizer ignored because different process') res = None else: sub_debug('finalizer calling %s with args %s and kwargs %s', self._callback, self._args, self._kwargs) res = self._callback(*self._args, **self._kwargs) self._weakref = self._callback = self._args = \ self._kwargs = self._key = None return res
Example 19
Project: trader Author: BigBrotherTrade File: main.py License: Apache License 2.0 | 6 votes |
def main(): loop = asyncio.get_event_loop() big_brother = None try: pid_path = os.path.join(app_dir.user_cache_dir, 'trader.pid') if not os.path.exists(pid_path): if not os.path.exists(app_dir.user_cache_dir): os.makedirs(app_dir.user_cache_dir) with open(pid_path, 'w') as pid_file: pid_file.write(str(os.getpid())) big_brother = TradeStrategy(io_loop=loop) print('Big Brother is watching you!') print('used config file:', config_file) print('log stored in:', app_dir.user_log_dir) print('pid file:', pid_path) loop.create_task(big_brother.install()) loop.run_forever() except KeyboardInterrupt: pass except Exception as ee: logger.info('发生错误: %s', repr(ee), exc_info=True) finally: big_brother and loop.run_until_complete(big_brother.uninstall()) logger.info('程序已退出')
Example 20
Project: neat-python Author: CodeReclaimers File: evolve_interactive.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def make_high_resolution(self, genome, config): genome_id, genome = genome # Make sure the output directory exists. if not os.path.isdir('rendered'): os.mkdir('rendered') if self.scheme == 'gray': image_data = eval_gray_image(genome, config, self.full_width, self.full_height) elif self.scheme == 'color': image_data = eval_color_image(genome, config, self.full_width, self.full_height) else: image_data = eval_mono_image(genome, config, self.full_width, self.full_height) image = self.make_image_from_data(image_data) pygame.image.save(image, "rendered/rendered-{}-{}.png".format(os.getpid(), genome_id)) with open("rendered/genome-{}-{}.bin".format(os.getpid(), genome_id), "wb") as f: pickle.dump(genome, f, 2)
Example 21
Project: evolution-strategies-starter Author: openai File: dist.py License: MIT License | 6 votes |
def retry_get(pipe, key, tries=300, base_delay=4.): for i in range(tries): # Try to (m)get if isinstance(key, (list, tuple)): vals = pipe.mget(key) if all(v is not None for v in vals): return vals else: val = pipe.get(key) if val is not None: return val # Sleep and retry if any key wasn't available if i != tries - 1: delay = base_delay * (1 + (os.getpid() % 10) / 9) logger.warning('{} not set. Retrying after {:.2f} sec ({}/{})'.format(key, delay, i + 2, tries)) time.sleep(delay) raise RuntimeError('{} not set'.format(key))
Example 22
Project: gist-alfred Author: danielecook File: util.py License: MIT License | 6 votes |
def atomic_writer(fpath, mode): """Atomic file writer. .. versionadded:: 1.12 Context manager that ensures the file is only written if the write succeeds. The data is first written to a temporary file. :param fpath: path of file to write to. :type fpath: ``unicode`` :param mode: sames as for :func:`open` :type mode: string """ suffix = '.{}.tmp'.format(os.getpid()) temppath = fpath + suffix with open(temppath, mode) as fp: try: yield fp os.rename(temppath, fpath) finally: try: os.remove(temppath) except (OSError, IOError): pass
Example 23
Project: ConvLab Author: ConvLab File: movie.py License: MIT License | 6 votes |
def __init__(self, spec, e=None, env_space=None): super(MovieEnv, self).__init__(spec, e, env_space) util.set_attr(self, self.env_spec, [ 'observation_dim', 'action_dim', ]) worker_id = int(f'{os.getpid()}{self.e+int(ps.unique_id())}'[-4:]) # TODO dynamically compose components according to env_spec self.u_env = MovieActInActOutEnvironment(worker_id) self.patch_gym_spaces(self.u_env) self._set_attr_from_u_env(self.u_env) # assert self.max_t is not None if env_space is None: # singleton mode pass else: self.space_init(env_space) logger.info(util.self_desc(self))
Example 24
Project: verge3d-blender-addon Author: Soft8Soft File: utils.py License: GNU General Public License v3.0 | 6 votes |
def make_msgid(idstring=None, domain=None): """Returns a string suitable for RFC 2822 compliant Message-ID, e.g: <20020201195627.33539.96671@nightshade.la.mastaler.com> Optional idstring if given is a string used to strengthen the uniqueness of the message id. Optional domain if given provides the portion of the message id after the '@'. It defaults to the locally defined hostname. """ timeval = time.time() utcdate = time.strftime('%Y%m%d%H%M%S', time.gmtime(timeval)) pid = os.getpid() randint = random.randrange(100000) if idstring is None: idstring = '' else: idstring = '.' + idstring if domain is None: domain = socket.getfqdn() msgid = '<%s.%s.%s%s@%s>' % (utcdate, pid, randint, idstring, domain) return msgid
Example 25
Project: verge3d-blender-addon Author: Soft8Soft File: profilehooks.py License: GNU General Public License v3.0 | 6 votes |
def __init__(self, fn, skip=0, filename=None, immediate=False, dirs=False, sort=None, entries=40, stdout=True): """Creates a profiler for a function. Every profiler has its own log file (the name of which is derived from the function name). HotShotFuncProfile registers an atexit handler that prints profiling information to sys.stderr when the program terminates. The log file is not removed and remains there to clutter the current working directory. """ if filename: self.logfilename = filename + ".raw" else: self.logfilename = "%s.%d.prof" % (fn.__name__, os.getpid()) super(HotShotFuncProfile, self).__init__( fn, skip=skip, filename=filename, immediate=immediate, dirs=dirs, sort=sort, entries=entries, stdout=stdout)
Example 26
Project: verge3d-blender-addon Author: Soft8Soft File: profilehooks.py License: GNU General Public License v3.0 | 6 votes |
def __init__(self, fn): """Creates a profiler for a function. Every profiler has its own log file (the name of which is derived from the function name). HotShotFuncCoverage registers an atexit handler that prints profiling information to sys.stderr when the program terminates. The log file is not removed and remains there to clutter the current working directory. """ self.fn = fn self.logfilename = "%s.%d.cprof" % (fn.__name__, os.getpid()) self.profiler = _hotshot.coverage(self.logfilename) self.ncalls = 0 atexit.register(self.atexit)
Example 27
Project: verge3d-blender-addon Author: Soft8Soft File: profilehooks.py License: GNU General Public License v3.0 | 6 votes |
def __init__(self, fn): """Creates a profiler for a function. Every profiler has its own log file (the name of which is derived from the function name). TraceFuncCoverage registers an atexit handler that prints profiling information to sys.stderr when the program terminates. The log file is not removed and remains there to clutter the current working directory. """ self.fn = fn self.logfilename = "%s.%d.cprof" % (fn.__name__, os.getpid()) self.ncalls = 0 atexit.register(self.atexit)
Example 28
Project: zun Author: openstack File: compute.py License: Apache License 2.0 | 6 votes |
def main(): priv_context.init(root_helper=shlex.split(utils.get_root_helper())) zun_service.prepare_service(sys.argv) LOG.info('Starting server in PID %s', os.getpid()) CONF.log_opt_values(LOG, logging.DEBUG) CONF.import_opt('topic', 'zun.conf.compute', group='compute') from zun.compute import manager as compute_manager endpoints = [ compute_manager.Manager(), ] server = rpc_service.Service.create(CONF.compute.topic, CONF.host, endpoints, binary='zun-compute') launcher = service.launch(CONF, server, restart_method='mutate') launcher.wait()
Example 29
Project: misp42splunk Author: remg427 File: utils.py License: GNU Lesser General Public License v3.0 | 6 votes |
def make_msgid(idstring=None, domain=None): """Returns a string suitable for RFC 2822 compliant Message-ID, e.g: <20020201195627.33539.96671@nightshade.la.mastaler.com> Optional idstring if given is a string used to strengthen the uniqueness of the message id. Optional domain if given provides the portion of the message id after the '@'. It defaults to the locally defined hostname. """ timeval = time.time() utcdate = time.strftime('%Y%m%d%H%M%S', time.gmtime(timeval)) pid = os.getpid() randint = random.randrange(100000) if idstring is None: idstring = '' else: idstring = '.' + idstring if domain is None: domain = socket.getfqdn() msgid = '<%s.%s.%s%s@%s>' % (utcdate, pid, randint, idstring, domain) return msgid
Example 30
Project: cherrypy Author: cherrypy File: plugins.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def start(self): pid = os.getpid() if self.finalized: self.bus.log('PID %r already written to %r.' % (pid, self.pidfile)) else: open(self.pidfile, 'wb').write(ntob('%s\n' % pid, 'utf8')) self.bus.log('PID %r written to %r.' % (pid, self.pidfile)) self.finalized = True