本文整理汇总了Python中multiprocessing.Process类的典型用法代码示例。如果您正苦于以下问题:Python Process类的具体用法?Python Process怎么用?Python Process使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Process类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect_multiprocess
def connect_multiprocess(service = VoidService, config = {}, remote_service = VoidService, remote_config = {}, args={}):
"""starts an rpyc server on a new process, bound to an arbitrary port,
and connects to it over a socket. Basically a copy of connect_thread().
However if args is used and if these are shared memory then changes
will be bi-directional. That is we now have access to shared memmory.
:param service: the local service to expose (defaults to Void)
:param config: configuration dict
:param server_service: the remote service to expose (of the server; defaults to Void)
:param server_config: remote configuration dict (of the server)
:param args: dict of local vars to pass to new connection, form {'name':var}
Contributed by *@tvanzyl*
"""
from multiprocessing import Process
listener = socket.socket()
listener.bind(("localhost", 0))
listener.listen(1)
def server(listener=listener, args=args):
client = listener.accept()[0]
listener.close()
conn = connect_stream(SocketStream(client), service = remote_service, config = remote_config)
try:
for k in args:
conn._local_root.exposed_namespace[k] = args[k]
conn.serve_all()
except KeyboardInterrupt:
interrupt_main()
t = Process(target = server)
t.start()
host, port = listener.getsockname()
return connect(host, port, service = service, config = config)
示例2: _find_active_serial_ports_from
def _find_active_serial_ports_from(self, wait_duration, device_files):
"""
Find and returns list of active USB serial ports.
This spawns a process that actually does the work.
Args:
device_files (list of strings):
List of device files that will be checked for serial ports.
Note that any other device file than ttyUSBx will be ignored.
Returns:
List of device files that have active serial port.
Example: ["ttyUSB2", "ttyUSB4", "ttyUSB7"]
"""
serial_results = Queue()
serial_finder = Process(
target=TopologyBuilder._get_active_serial_device_files,
args=(self, serial_results, wait_duration, device_files))
if self._verbose:
print "Serial thread - Finding active serial ports"
logging.info("Finding active serial ports")
serial_finder.start()
return serial_results
示例3: benchmark
def benchmark(self, request, pk):
queryset = Attempt.objects.all()
attempt = get_object_or_404(queryset, id=pk)
serializer = AttemptSerializer(attempt)
# check payload
payload = dict(request.data)
if 'database' not in payload and 'benchmark' not in payload:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# run benchmark
process = Process(target = utils.run_benchmark, args = (pk, payload['database'], payload['benchmark']))
process.start()
# utils.run_benchmark(pk, payload['database'], payload['benchmark'])
# shoule know the deployer id
deployer_id = 1
log_file_path = os.path.join(os.path.dirname(__file__), os.pardir, 'vagrant', str(deployer_id) + '.log')
def stream_response_generator():
last_line_no = 0
while process.is_alive():
time.sleep(1)
with open(log_file_path, 'r') as log_file:
content = log_file.readlines()
line_no = len(content)
if line_no > last_line_no:
yield ''.join(content[last_line_no:])
last_line_no = line_no
time.sleep(1)
with open(log_file_path, 'r') as log_file:
content = log_file.readlines()
line_no = len(content)
if line_no > last_line_no:
yield ''.join(content[last_line_no:])
return StreamingHttpResponse(stream_response_generator())
示例4: __init__
def __init__(self, func):
Process.__init__(self)
self.in_buffer = None
self.out_buffer = None
self.func = func
# number of tokens popped from the input buffer each time
self.n_args = len(inspect.getargspec(func).args)
示例5: start_echo_server_process
def start_echo_server_process():
# XXX DO NOT FORGET TO KILL THE PROCESS IF THE TEST DOES NOT SUCCEED
sleep()
p = Process(target=start_echo_server)
p.start()
sleep(1.5)
return p
示例6: main
def main():
warnings.filterwarnings("ignore", "Degrees of freedom <= 0 for slice", RuntimeWarning)
options = getoptions()
setuplogger(options['log'], options['logfile'], logging.INFO)
total_procs = options['nprocs'] * options['total_instances']
start_offset = options['instance_id'] * options['nprocs']
exit_code = 0
if options['nprocs'] == 1:
createsummary(options, None, None)
else:
proclist = []
for procid in xrange(options['nprocs']):
p = Process( target=createsummary, args=(options, total_procs, start_offset + procid) )
p.start()
proclist.append(p)
for proc in proclist:
proc.join()
exit_code += proc.exitcode
sys.exit(exit_code)
示例7: processFiles
def processFiles(patch_dir):
root = os.getcwd()
glbl.data_dirs = {}
if root != patch_dir: working_path = root+"/"+patch_dir
else: working_path = root
for path, dirs, files in os.walk(working_path):
if len(dirs) == 0: glbl.data_dirs[path] = ''
# Multiprocessing Section
#########################################
Qids = glbl.data_dirs.keys()
manager = Manager() # creates shared memory manager object
results = manager.dict() # Add dictionary to manager, so it can be accessed across processes
nextid = Queue() # Create Queue object to serve as shared id generator across processes
for qid in Qids: nextid.put(qid) # Load the ids to be tested into the Queue
for x in range(0,multiprocessing.cpu_count()): # Create one process per logical CPU
p = Process(target=processData, args=(nextid,results)) # Assign process to processCBR function, passing in the Queue and shared dictionary
glbl.jobs.append(p) # Add the process to a list of running processes
p.start() # Start process running
for j in glbl.jobs:
j.join() # For each process, join them back to main, blocking on each one until finished
# write out results
c = 1
sets = results.keys()
sets.sort()
for x in sets:
if results[x] != 'None':
FINAL = open('result'+str(c)+'.txt','w')
n = "\n************************************************************************************************\n"
FINAL.write(n+"* "+x+' *\n'+n+results[x]+"\n")
FINAL.close()
c += 1
示例8: __init__
def __init__(self, response_queue, backup_name, host_port, user, password, authdb, base_dir, binary,
dump_gzip=False, verbose=False):
Process.__init__(self)
self.host, port = host_port.split(":")
self.host_port = host_port
self.port = int(port)
self.response_queue = response_queue
self.backup_name = backup_name
self.user = user
self.password = password
self.authdb = authdb
self.base_dir = base_dir
self.binary = binary
self.dump_gzip = dump_gzip
self.verbose = verbose
self._command = None
self.completed = False
self.backup_dir = "%s/%s" % (self.base_dir, self.backup_name)
self.dump_dir = "%s/dump" % self.backup_dir
self.oplog_file = "%s/oplog.bson" % self.dump_dir
self.start_time = time()
signal(SIGINT, self.close)
signal(SIGTERM, self.close)
示例9: serve
def serve(self):
"""Start a fixed number of worker threads and put client into a queue"""
#this is a shared state that can tell the workers to exit when set as false
self.isRunning.value = True
#first bind and listen to the port
self.serverTransport.listen()
#fork the children
for i in range(self.numWorkers):
try:
w = Process(target=self.workerProcess)
w.daemon = True
w.start()
self.workers.append(w)
except (Exception) as x:
logging.exception(x)
#wait until the condition is set by stop()
while True:
self.stopCondition.acquire()
try:
self.stopCondition.wait()
break
except (SystemExit, KeyboardInterrupt):
break
except (Exception) as x:
logging.exception(x)
self.isRunning.value = False
示例10: run_stock_parser
def run_stock_parser():
symbol_q = Queue()
price_q = Queue()
stock_symbols = []
with open('symbols.txt', 'r') as symfile:
for n, line in enumerate(symfile):
sym = line.strip()
if sym:
stock_symbols.append(sym)
ncpu = len([x for x in open('/proc/cpuinfo').read().split('\n')\
if x.find('processor') == 0])
pool = [Process(target=read_stock_worker, args=(symbol_q, price_q, )) for _ in range(ncpu * 4)]
for p in pool:
p.start()
output = Process(target=write_output_file, args=(price_q, ))
output.start()
for symbol in stock_symbols:
symbol_q.put(symbol)
symbol_q.put(_sentinel)
for p in pool:
p.join()
price_q.put(_sentinel)
output.join()
示例11: MultiProcessPlot
class MultiProcessPlot(object):
## Initilization
def __init__(self):
self.plotpipe, PlotterPipe = Pipe()
## Called process for plotting
self.plotter = ProcessPlotter()
## Process holder
self.plotprocess = Process(target = self.plotter, args = (PlotterPipe, ))
self.plotprocess.daemon = True
self.plotprocess.start()
## Plot function
def plot(self, finished=False):
send = self.plotpipe.send
if finished:
send(None)
else:
if not LoopCounter % plotRefreshPeriod:
reset = 1
else:
reset = 0
## Compose data for pipe
data = [reset,
MessageMeasurement.pose2d.x, MessageMeasurement.pose2d.y, MessageMeasurement.pose2d.theta,
MessageEKF.odompose2d.x, MessageEKF.odompose2d.y, MessageEKF.odompose2d.theta,
MessageEKF.ekfpose2d.x, MessageEKF.ekfpose2d.y, MessageEKF.ekfpose2d.theta]
# print(MessageEKF.ekfpose2d.x, MessageEKF.ekfpose2d.y, MessageEKF.ekfpose2d.theta) # //VB
# print(MessageEKF.odompose2d.x, MessageEKF.odompose2d.y, MessageEKF.odompose2d.theta) # //VB
## Send data through pipe
send(data)
## Reset global flags to receive new input
flagSubscriber1 = False
flagSubscriber2 = False
示例12: start_parser_process
def start_parser_process(self):
if self.mp_mode:
from multiprocessing import Process, Event
else:
from multiprocessing.dummy import Process, Event
waiting_shutdown_event = Event()
if self.mp_mode:
bot = self.bot.__class__(
network_result_queue=self.network_result_queue,
parser_result_queue=self.parser_result_queue,
waiting_shutdown_event=waiting_shutdown_event,
shutdown_event=self.shutdown_event,
parser_mode=True,
meta=self.bot.meta)
else:
# In non-multiprocess mode we start `run_process`
# method in new semi-process (actually it is a thread)
# Because the use `run_process` of main spider instance
# all changes made in handlers are applied to main
# spider instance, that allows to suppport deprecated
# spiders that do not know about multiprocessing mode
bot = self.bot
bot.network_result_queue = self.network_result_queue
bot.parser_result_queue = self.parser_result_queue
bot.waiting_shutdown_event = waiting_shutdown_event
bot.shutdown_event = self.shutdown_event
bot.meta = self.bot.meta
proc = Process(target=bot.run_parser)
if not self.mp_mode:
proc.daemon = True
proc.start()
return waiting_shutdown_event, proc
示例13: apply_update
def apply_update(fname, status):
# As soon as python-apt closes its opened files on object deletion
# we can drop this fork workaround. As long as they keep their files
# open, we run the code in an own fork, than the files are closed on
# process termination an we can remount the filesystem readonly
# without errors.
p = Process(target=_apply_update, args=(fname, status))
with rw_access("/", status):
try:
t_ver = get_target_version(fname)
except BaseException:
status.log('Reading xml-file failed!')
return
try:
c_ver = get_current_version()
except IOError as e:
status.log('get current version failed: ' + str(e))
c_ver = ""
pre_sh(c_ver, t_ver, status)
p.start()
p.join()
status.log("cleanup /var/cache/apt/archives")
# don't use execute() here, it results in an error that the apt-cache
# is locked. We currently don't understand this behaviour :(
os.system("apt-get clean")
if p.exitcode != 0:
raise Exception(
"Applying update failed. See logfile for more information")
post_sh(c_ver, t_ver, status)
示例14: __init__
def __init__(self, dt=1):
import psutil
Process.__init__(self)
self.daemon = True
self.dt = dt
self.parent = psutil.Process(current_process().pid)
self.parent_conn, self.child_conn = Pipe()
示例15: __init__
def __init__(self, worker, outlist, index):
Process.__init__(self)
self.worker = worker
if worker is None or worker.element is None:
raise MultiProjectException("Bug: Invalid Worker")
self.outlist = outlist
self.index = index