本文整理汇总了Python中os.getpid方法的典型用法代码示例。如果您正苦于以下问题:Python os.getpid方法的具体用法?Python os.getpid怎么用?Python os.getpid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.getpid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ingest_file
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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))
示例2: atomic_writer
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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
示例3: __init__
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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()
示例4: _is_daemonized
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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())
)
示例5: get_memory_usage
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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
示例6: start_data_processing
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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)
示例7: run
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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()
示例8: run
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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')
示例9: drop_privileges
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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)
示例10: write_app
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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
示例11: _check_alive
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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
示例12: get_config
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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
示例13: testInterruptCaught
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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)
示例14: testSecondInterrupt
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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)
示例15: testTwoResults
# 需要导入模块: import os [as 别名]
# 或者: from os import getpid [as 别名]
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)