本文整理匯總了Python中multiprocessing.process方法的典型用法代碼示例。如果您正苦於以下問題:Python multiprocessing.process方法的具體用法?Python multiprocessing.process怎麽用?Python multiprocessing.process使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類multiprocessing
的用法示例。
在下文中一共展示了multiprocessing.process方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def __init__(self):
global _all_handles
# Generate label of text/unicode type from three random bytes.
self._id = codecs.encode(os.urandom(3), "hex_codec").decode("ascii")
self._legit_pid = os.getpid()
self._make_nonblocking()
# Define lock for synchronizing access to this handle within the current
# process. Note that a `gevent.lock.Semaphore` instance lives on the
# heap of the current process and cannot be used to synchronize access
# across multiple processes. That is, this lock is only meaningful in
# the current process. This is especially important to consider when the
# platform supports fork()ing.
self._lock = gevent.lock.Semaphore(value=1)
self._closed = False
_all_handles.append(self)
示例2: _validate
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def _validate(self):
"""Raise exception if this handle is closed or not registered to be
used in the current process.
Intended to be called before every operation on `self._fd`.
Reveals wrong usage of this module in the context of multiple
processes. Might prevent tedious debugging sessions. Has little
performance impact.
"""
if self._closed:
raise GIPCClosed(
"GIPCHandle has been closed before.")
if os.getpid() != self._legit_pid:
raise GIPCError(
"GIPCHandle %s not registered for current process %s." % (
self, os.getpid()))
示例3: _winapi_childhandle_after_createprocess_child
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def _winapi_childhandle_after_createprocess_child(self):
"""Called on Windows in the child process after the CreateProcess()
system call. This is required for making the handle usable in the child.
"""
if WINAPI_HANDLE_TRANSFER_STEAL:
# In this case the handle has not been inherited by the child
# process during CreateProcess(). Steal it from the parent.
new_winapihandle = multiprocessing.reduction.steal_handle(
self._parent_pid, self._parent_winapihandle)
del self._parent_winapihandle
del self._parent_pid
# Restore C file descriptor with (read/write)only flag.
self._fd = msvcrt.open_osfhandle(new_winapihandle, self._fd_flag)
return
# In this case the handle has been inherited by the child process during
# the CreateProcess() system call. Get C file descriptor from Windows
# file handle.
self._fd = msvcrt.open_osfhandle(
self._inheritable_winapihandle, self._fd_flag)
del self._inheritable_winapihandle
示例4: run_trial
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def run_trial(self, trial_num, param):
'''
algo step 2, construct and run Trial with the next param
args trial_num, param must be provided externally,
otherwise they will not progress within mp.process
'''
experiment_spec = self.compose_experiment_spec(param)
trial = self.Trial(
experiment_spec, trial_num=trial_num,
times=self.times,
num_of_trials=self.num_of_trials,
run_timestamp=self.run_timestamp,
experiment_id_override=self.experiment_id_override)
trial_data = trial.run()
del trial
import gc
gc.collect()
debug_mem_usage()
return trial_data
# retrieve the trial_num, param, fitness_score from trial_data
示例5: startASLogger
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def startASLogger(loggerAddr, logDefs, transport, capabilities,
aggregatorAddress=None,
concurrency_context = None):
endpointPrep = transport.prepEndpoint(loggerAddr, capabilities)
multiprocessing.process._current_process._daemonic = False
NewProc = concurrency_context.Process if concurrency_context else multiprocessing.Process
logProc = NewProc(target=startupASLogger,
args = (transport.myAddress, endpointPrep,
logDefs,
transport.__class__, aggregatorAddress))
logProc.daemon = True
logProc.start()
transport.connectEndpoint(endpointPrep)
# When the caller that owns the transport starts their run(), it
# will receive the LoggerConnected from the child to complete the
# handshake and the sender will be the actual address of the
# logger.
return ChildInfo(loggerAddr, 'logger', logProc, endpointPrep.addrInst)
示例6: get_multiprocessing_process__dangling
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def get_multiprocessing_process__dangling(self):
if not multiprocessing:
return None
# This copies the weakrefs without making any strong reference
return multiprocessing.process._dangling.copy()
示例7: restore_multiprocessing_process__dangling
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def restore_multiprocessing_process__dangling(self, saved):
if not multiprocessing:
return
multiprocessing.process._dangling.clear()
multiprocessing.process._dangling.update(saved)
示例8: bind_port
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def bind_port(sock, host=HOST):
"""Bind the socket to a free port and return the port number. Relies on
ephemeral ports in order to ensure we are using an unbound port. This is
important as many tests may be running simultaneously, especially in a
buildbot environment. This method raises an exception if the sock.family
is AF_INET and sock.type is SOCK_STREAM, *and* the socket has SO_REUSEADDR
or SO_REUSEPORT set on it. Tests should *never* set these socket options
for TCP/IP sockets. The only case for setting these options is testing
multicasting via multiple UDP sockets.
Additionally, if the SO_EXCLUSIVEADDRUSE socket option is available (i.e.
on Windows), it will be set on the socket. This will prevent anyone else
from bind()'ing to our host/port for the duration of the test.
"""
if sock.family == socket.AF_INET and sock.type == socket.SOCK_STREAM:
if hasattr(socket, 'SO_REUSEADDR'):
if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) == 1:
raise TestFailed("tests should never set the SO_REUSEADDR " \
"socket option on TCP/IP sockets!")
if hasattr(socket, 'SO_REUSEPORT'):
try:
if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 1:
raise TestFailed("tests should never set the SO_REUSEPORT " \
"socket option on TCP/IP sockets!")
except socket.error:
# Python's socket module was compiled using modern headers
# thus defining SO_REUSEPORT but this process is running
# under an older kernel that does not support SO_REUSEPORT.
pass
if hasattr(socket, 'SO_EXCLUSIVEADDRUSE'):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
sock.bind((host, 0))
port = sock.getsockname()[1]
return port
示例9: temp_umask
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def temp_umask(umask):
"""Context manager that temporarily sets the process umask."""
oldmask = os.umask(umask)
try:
yield
finally:
os.umask(oldmask)
示例10: strip_python_stderr
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def strip_python_stderr(stderr):
"""Strip the stderr of a Python process from potential debug output
emitted by the interpreter.
This will typically be run on the result of the communicate() method
of a subprocess.Popen object.
"""
stderr = re.sub(br"\[\d+ refs\]\r?\n?", b"", stderr).strip()
return stderr
示例11: _cooperative_process_close_unix
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def _cooperative_process_close_unix(self):
"""
For compatibility with CPython 3.7+ where this method was introduced.
Also see https://bugs.python.org/issue30596 for the discussion that
led to introducing this method.
The main motivation of this method is to release a tiny amount of resources
_now_ instead of later through GC, and to then set a few properties
indicating that the process object cannot be used for managing an actual
process anymore.
The original implementation uses `self._popen.poll() is None` to check if
the underlying process is still running (and raises a ValueError in that
case). That is a synchronous check for the question whether the process is
still running or not. In the context of gipc it is not allowed to call
`self._popen.poll()` (as of a waitpid() race against libev, see above). That
is why this method is re-implemented cooperatively here.
"""
if self._popen is not None:
if self._popen.returncode is None:
# The error message is copied verbatim from stdlib (3.7).
raise ValueError(
"Cannot close a process while it is still running. "
"You should first call join() or terminate().")
self._popen.close()
self._popen = None
del self._sentinel
multiprocessing.process._children.discard(self)
self._closed = True
示例12: start
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def start(self):
# Start grabbing SIGCHLD within libev event loop.
gevent.get_hub().loop.install_sigchld()
# Run new process (based on `fork()` on POSIX-compliant systems).
super(_GProcess, self).start()
# The occurrence of SIGCHLD is recorded asynchronously in libev.
# This guarantees proper behavior even if the child watcher is
# started after the child exits. Start child watcher now.
self._sigchld_watcher = gevent.get_hub().loop.child(self.pid)
self._returnevent = gevent.event.Event()
self._sigchld_watcher.start(
self._on_sigchld, self._sigchld_watcher)
log.debug("SIGCHLD watcher for %s started.", self.pid)
示例13: __repr__
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def __repr__(self):
"""Based on original __repr__ from CPython 3.4's mp package.
Reasons for re-implementing:
* The original code would invoke os.waitpid() through
_popen.poll(). This is forbidden in the context of gipc.
This method instead reads the exitcode property which is set
asynchronously by a libev child watcher callback.
* The original code distinguishes 'initial' state from 'started'
state. This is not necessary, as gipc starts processes right
away.
* This method removes the `if self is _current_process` check
without changing output behavior (that's still 'started' status).
"""
exitcodedict = multiprocessing.process._exitcode_to_name
status = 'started'
if self._parent_pid != os.getpid():
status = 'unknown'
elif self.exitcode is not None:
status = self.exitcode
if status == 0:
status = 'stopped'
elif isinstance(status, int):
status = 'stopped[%s]' % exitcodedict.get(status, status)
return '<%s(%s, %s%s)>' % (
type(self).__name__,
self._name,
status,
self.daemon and ' daemon' or ''
)
示例14: join
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def join(self, timeout=None):
"""
Wait cooperatively until child process terminates or timeout occurs.
:arg timeout: ``None`` (default) or a a time in seconds. The method
simply returns upon timeout expiration. The state of the process
has to be identified via ``is_alive()``.
"""
self._check_closed()
assert self._parent_pid == os.getpid(), "I'm not parent of this child."
assert self._popen is not None, 'Can only join a started process.'
if not WINDOWS:
# Resemble multiprocessing's join() method while replacing
# `self._popen.wait(timeout)` with
# `self._returnevent.wait(timeout)`
self._returnevent.wait(timeout)
if self._popen.returncode is not None:
if hasattr(multiprocessing.process, '_children'):
# This is for Python 3.4 and beyond.
kids = multiprocessing.process._children
else:
# For Python 2.6, 2.7, 3.3.
kids = multiprocessing.process._current_process._children
kids.discard(self)
return
with gevent.Timeout(timeout, False):
while self.is_alive():
# This frequency seems reasonable, but that's not 100 % certain.
gevent.sleep(0.01)
# Clean up after child as designed by Process class (non-blocking).
super(_GProcess, self).join(timeout=0)
示例15: _winapi_childhandle_after_createprocess_parent
# 需要導入模塊: import multiprocessing [as 別名]
# 或者: from multiprocessing import process [as 別名]
def _winapi_childhandle_after_createprocess_parent(self):
"""Called on Windows in the parent process after the CreateProcess()
system call. This method is intended to revert the actions performed
within `_winapi_childhandle_prepare_transfer()`. In particular, this
method is intended to prepare a subsequent call to the handle's
`close()` method.
"""
if WINAPI_HANDLE_TRANSFER_STEAL:
del self._parent_winapihandle
del self._parent_pid
# Setting `_fd` to None prevents the subsequent `close()` method
# invocation (triggered in `start_process()` after child creation)
# from actually calling `os.close()` on the file descriptor. This
# must be prevented because at this point the handle either already
# is or will be "stolen" by the child via a direct WinAPI call using
# the DUPLICATE_CLOSE_SOURCE option (and therefore become
# auto-closed, here, in the parent). The relative timing is not
# predictable. If the child process steals first, os.close() here
# would result in `OSError: [Errno 9] Bad file descriptor`. If
# os.close() is called on the handle in the parent before the child
# can steal the handle, a `OSError: [WinError 6] The handle is
# invalid` will be thrown in the child upon the stealing attempt.
self._fd = None
return
# Get C file descriptor from Windows file handle.
self._fd = msvcrt.open_osfhandle(
self._inheritable_winapihandle, self._fd_flag)
del self._inheritable_winapihandle