本文整理汇总了Python中multiprocessing.Pipe.close方法的典型用法代码示例。如果您正苦于以下问题:Python Pipe.close方法的具体用法?Python Pipe.close怎么用?Python Pipe.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.Pipe
的用法示例。
在下文中一共展示了Pipe.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_pipe
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
def process_pipe():
parent_conn, child_conn = Pipe()
p = Process(target=pipe_test, args=(child_conn,))
p.start()
print parent_conn.recv()
p.join()
parent_conn.close()
示例2: __init__
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
class PluginRunner:
def __init__(self, plugin):
self.name = plugin
self.proc = None
self.running = False
self.local_pipe, self.remote_pipe = Pipe()
def getConnection(self):
return self.local_pipe
def start(self):
assert not self.running, "Already running."
self.running = True
self.thread = Thread(target=self.run)
self.thread.start()
def restart(self):
self.proc.terminate()
def stop(self):
assert self.running, "Running"
self.running = False
self.proc.terminate()
self.thread.join()
self.remote_pipe.close()
self.local_pipe.close()
def run(self):
while self.running:
self.proc = Process(target=launch, args=('repeat', self.remote_pipe))
self.proc.start()
print("Waiting on proc to end")
self.proc.join()
示例3: MMLWorker
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
class MMLWorker(object):
"""Represents the Daemon's connection to the subprocess"""
def __init__(self,runner):
"""Creates and initalizes a subprocess and its connections."""
self.pipe, pipe = Pipe()
self.proc = Process(target=worker, args=(pipe,runner))
self.proc.start();
self.pid = self.proc.pid
def __del__(self):
"""Ensures the subprocess is correctly garbage collected."""
self.pipe.close();
self.proc.terminate();
def pump(self,block=False):
"""Returns a key,val pair from the subprocess, or None,None."""
key,val = None,None
if block:
(key,val) = self.pipe.recv()
elif self.pipe.poll():
(key,val) = self.pipe.recv()
return key,val
def stop(self):
"""Sends the stop signal to the subprocess."""
self.pipe.send(('stop',()))
def pause(self):
"""Sends the pause signal to the subprocess."""
self.pipe.send(('pause',()))
def start(self,program):
"""Sends the start signal to the subprocess."""
self.pipe.send(('start',(program,)))
示例4: dispatch
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
def dispatch(self, fcn, timeout=0, params=()):
"""Start a new subrocess.
This function will detach 'fcn' as a process of its own,
passing params AND a pipe as arguments to 'fcn'.
If 'timeout' > 0 this function will block, waiting for the
detached process to send some data over the pipe.
If data i written to pipe in time, the pipe will be returned to the calling process.
On timeout, the pipe will be closed and 'None' will be returned to the calling process.
"""
conn, child_conn = Pipe()
p = Process(target=fcn, args=params + (child_conn,))
p.start()
if timeout > 0:
poll_status = conn.poll(timeout)
if poll_status == False:
print "Dispatched function %s did not send anything within the specified timeout (%d s)" % (fcn, timeout)
# FIXME: How to properly handle this case?
# - p.terminate() doesn't terminate any subprocesses the p might have spawned
# - conn.close(), i.e. closing the control channel, is probably better. Make sure p detects and handle it.
# - send a proper control message (set running to False) should work too.
# The proper way to implement a client is to handle connection.close() and stop running control message in the same way.
conn.close()
conn = None
return conn
示例5: transcode
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
def transcode(self, path, format='mp3', bitrate=False):
if self.stopping.is_set():
return
try:
stop = Event()
start_time = time.time()
parent_conn, child_conn = Pipe()
process = Process(target=transcode_process,
args=(child_conn, path, stop, format, bitrate))
process.start()
while not (self.stopping.is_set() or stop.is_set()):
data = parent_conn.recv()
if not data:
break
yield data
logger.debug("Transcoded %s in %0.2f seconds." % (path.encode(cfg['ENCODING']), time.time() - start_time))
except GeneratorExit:
stop.set()
logger.debug("User canceled the request during transcoding.")
except:
stop.set()
logger.warn("Some type of error occured during transcoding.")
finally:
parent_conn.close()
process.join()
示例6: test_queue_pipe
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
def test_queue_pipe():
print 'testing for pipe:'
for count in [10, 10**4, 10**5]:
output_p, input_p = Pipe()
#break
reader_p = Process(target=reader_pipe, args=((output_p, input_p),))
reader_p.start()
output_p.close()
_start = time.time()
writer_pipe(count, input_p)
input_p.close()
reader_p.join()
print 'Sending %s numbers to Pipe() took %s seconds' % (count, (time.time() - _start))
break
print "testing for queue:"
for count in [10**3, 10**4, 10**5]:
queue = Queue()
reader_p = Process(target=reader_queue, args=((queue),))
reader_p.daemon = True
reader_p.start()
_start = time.time()
writer_queue(count, queue)
reader_p.join()
print "Sending %s numbers to Queue() took %s seconds" % (count, (time.time() - _start))
示例7: run
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
def run(self):
parent_pipe, child_pipe = Pipe(False)
p = Process(target = run_metaheuristic,
args = (child_pipe, self.model, self.pt, self.aa,
self.algo, self.n, self.use_heur,
self.worst, self.best))
p.start()
for i in range(self.n + 1):
if self.is_stopped() is True:
parent_pipe.close()
p.terminate()
return
try:
result = parent_pipe.recv()
except:
break
self.results.append(result[0])
self.fitness.append(result[1])
self.emit(QtCore.SIGNAL('update(int)'), i)
if result[1] == 1:
break
parent_pipe.close()
p.join()
示例8: calculateArea
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
def calculateArea(feature,session_cookies,options):
"""
return:{
status {
"invalid" : invalid message;
"failed" : failed message;
"overlapped" : overlap message
}
data: {
total_area: 100 //exist if status_code = 1
other_area: 10 //exist if status_code = 1 and len(layers) > 0
layers: { //exist if status_code = 1 and len(layers) > 0
layer id: {
total_area: 12
areas:[
{area:1, properties:{
name:value
}}
]
}
}
}
}
"""
parent_conn,child_conn = Pipe(True)
p = Process(target=calculateAreaInProcess,args=(child_conn,))
p.daemon = True
p.start()
parent_conn.send([feature,session_cookies,options])
result = parent_conn.recv()
parent_conn.close()
#p.join()
#print("{}:get the area result from other process".format(datetime.now()))
return result
示例9: run
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
class ProcessHandler:
'''
run(): The run() method is the entry point for a thread.
start(): The start() method starts a thread by calling the run method.
join([time]): The join() waits for threads to terminate.
isAlive(): The isAlive() method checks whether a thread is still executing.
'''
def __init__(self, daemonic, pipe):
self.daemonic = daemonic
self.pipe = pipe
if self.pipe:
self.parent_conn, self.child_conn = Pipe(duplex=False)
@abc.abstractmethod
def run(self, *args):
pass
def start(self, *args):
# Close write fd because parent not going to write
if not self.pipe:
self.process = Process(target=self.run, args=args)
else:
self.process = Process(
target=self.run, args=(self.child_conn,) + args)
if self.daemonic:
self.process.daemon = True
self.process.start()
def join(self):
if self.pipe:
self.parent_conn.close()
self.child_conn.close()
self.process.join()
"""
示例10: IndexProxy
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
class IndexProxy(Thread):
def __init__(self, network, indexdir, cmdprefix):
Thread.__init__(self)
self.module_p, index_p = Pipe()
self.proc = IndexProcess(network, indexdir, index_p)
self.proc.start()
self.inqueue = Queue() # thread.ident, query/data
self.waiting = {} #threadID : queue
self.cmdprefix = cmdprefix
def run(self):
procpipe = self.module_p
while True:
# process module calls
try: type, data = self.inqueue.get(timeout=0.2)
except Empty: pass
else:
try:
#process queued item
if type == STOP:
self.module_p.close()
break
elif type == QUERY:
resq, threadident = data[0:2]
data = data[1:]
self.waiting[threadident] = resq
procpipe.send((type, data))
else:
procpipe.send((type, data))
except:
print_exc()
prnt("IndexProxy Exception in pump.")
# process pipe data
while procpipe.poll():
tid, result = procpipe.recv()
try:
self.waiting.pop(tid).put(result)
except KeyError:
prnt("WAITING THREAD ID NOT FOUND FOR RESULT:"+repr(result))
for queue in self.waiting.itervalues():
queue.put(None)
def search(self, source, query, n, gb=None):
""" Will return None if shutdown before response ready."""
resultq = Queue()
self.inqueue.put((QUERY, (resultq, current_thread().ident, source, query, n, gb)))
return resultq.get()
def logmsg(self, *args):
# Ignore all lines that start with commandprefix, but allow things like "... (etc)"
if args[-1][0] == self.cmdprefix and args[-1][1] != self.cmdprefix: return
self.inqueue.put((LOG, args))
def stop(self):
self.inqueue.put((STOP, None))
# old, new
def rename(self, *args):
self.inqueue.put((RENAME, args))
示例11: _ask
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
def _ask(self, msg, args=(), kwargs={}):
i, o = Pipe()
reduced = reduce_connection(i)
self.inbox.put([msg, args, kwargs, reduced[1]])
ret = o.recv()
i.close()
o.close()
return ret
示例12: _proxy_loop
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
def _proxy_loop(self, broker, *args):
is_debug = partial(log.isEnabledFor, logging.DEBUG)
pid = os.getpid()
proc = None
queue = self.queue
def stop():
try:
if proc is not None:
child.send(STOP)
child.close()
proc.join()
except Exception:
log.error('%s failed to stop cleanly',
str(self), exc_info=True)
raise
else:
log.debug('terminated %s', str(self))
finally:
self.pid = '%s-terminated' % self.pid
while True:
if proc is None or not proc.is_alive():
# start new worker process
child, parent = Pipe()
cx = _reduce_connection(parent) # HACK reduce for pickle
proc = run_in_subprocess(worker_process, pid, cx, *args)
self.pid = proc.pid
task, return_to_pool = queue.get()
if task == STOP:
stop()
break
try:
child.send(task)
while not child.poll(task.heartrate):
if not proc.is_alive():
broker.task_failed(task)
raise Error('unknown cause of death')
broker.heartbeat(task)
(result, status) = child.recv()
broker.set_result(task, result)
except Exception:
log.error('%s died unexpectedly', str(self), exc_info=True)
child.close()
proc.stdin.close()
proc = None
else:
if is_debug():
log.debug('%s completed task', str(self))
if status == STOP:
child.close()
proc.stdin.close()
proc = None
finally:
return_to_pool(self)
示例13: test_mount_fstab_user_fail
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
def test_mount_fstab_user_fail(self):
if not self._can_create:
self.skipTest('Cannot create %s filesystem' % self._fs_name)
if not self._can_mount:
self.skipTest('Cannot mount %s filesystem' % self._fs_name)
# this test will change /etc/fstab, we might want to revert the changes after it finishes
fstab = self.read_file('/etc/fstab')
self.addCleanup(self.write_file, '/etc/fstab', fstab)
disk = self.get_object('/block_devices/' + os.path.basename(self.vdevs[0]))
self.assertIsNotNone(disk)
# create filesystem
disk.Format(self._fs_name, self.no_options, dbus_interface=self.iface_prefix + '.Block')
self.addCleanup(self._clean_format, disk)
# create user for our test
self.addCleanup(self._remove_user, self.username)
uid, gid = self._add_user(self.username)
# add unmount cleanup now in case something wrong happens in the other process
self.addCleanup(self._unmount, self.vdevs[0])
# create pipe to get error (if any)
parent_conn, child_conn = Pipe()
proc = Process(target=self._mount_as_user_fstab_fail, args=(child_conn, int(uid), int(gid), self.vdevs[0]))
proc.start()
res = parent_conn.recv()
parent_conn.close()
proc.join()
if not res[0]:
self.fail(res[1])
# now mount it as root and test that user can't unmount it
mnt_path = disk.Mount(self.no_options, dbus_interface=self.iface_prefix + '.Filesystem')
self.assertIsNotNone(mnt_path)
self.assertTrue(os.path.ismount(mnt_path))
# create pipe to get error (if any)
parent_conn, child_conn = Pipe()
proc = Process(target=self._unmount_as_user_fstab_fail, args=(child_conn, int(uid), int(gid), self.vdevs[0]))
proc.start()
res = parent_conn.recv()
parent_conn.close()
proc.join()
if not res[0]:
self.fail(res[1])
self.assertTrue(os.path.ismount(mnt_path))
self._unmount(mnt_path)
示例14: _Tracker
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
class _Tracker(Process):
"""Background process for tracking resource usage"""
def __init__(self, dt=1):
Process.__init__(self)
self.daemon = True
self.dt = dt
self.parent_pid = current_process().pid
self.parent_conn, self.child_conn = Pipe()
def shutdown(self):
if not self.parent_conn.closed:
self.parent_conn.send('shutdown')
self.parent_conn.close()
self.join()
def _update_pids(self, pid):
return [self.parent] + [p for p in self.parent.children()
if p.pid != pid and p.status() != 'zombie']
def run(self):
psutil = import_required("psutil", "Tracking resource usage requires "
"`psutil` to be installed")
self.parent = psutil.Process(self.parent_pid)
pid = current_process()
data = []
while True:
try:
msg = self.child_conn.recv()
except KeyboardInterrupt:
continue
if msg == 'shutdown':
break
elif msg == 'collect':
ps = self._update_pids(pid)
while not data or not self.child_conn.poll():
tic = default_timer()
mem = cpu = 0
for p in ps:
try:
mem2 = p.memory_info().rss
cpu2 = p.cpu_percent()
except Exception: # could be a few different exceptions
pass
else:
# Only increment if both were successful
mem += mem2
cpu += cpu2
data.append((tic, mem / 1e6, cpu))
sleep(self.dt)
elif msg == 'send_data':
self.child_conn.send(data)
data = []
self.child_conn.close()
示例15: guiconnector
# 需要导入模块: from multiprocessing import Pipe [as 别名]
# 或者: from multiprocessing.Pipe import close [as 别名]
class guiconnector():
def __init__(self, console, observer, modelsready):
self.console = console
self.observer = observer
self.modelsready = modelsready
# Starts up data structures
def start(self, params):
self.a, self.b = Pipe(duplex=True)
if __name__ == "projectb.maingui":
self.p = Process(target=BayesianOptProcess, kwargs={
"params": parseintosimple(params),
"pipein": self.a,
"pipeout": self.b
})
self.t = threading.Thread(target=self.bayesoptlistener)
self.t.start()
self.p.start()
# Listnere for data from the BayesOpt Process
def bayesoptlistener(self):
pipein = self.a
pipeout = self.b
stop = False;
while not stop:
if pipein.poll():
output = pipein.recv()
if output.has_key("stop_connector"):
break
if output.has_key("stopped_bayes"):
self.modelsready(output["stopped_bayes"])
for k, v in output.items():
if k == "console":
self.console.log(v["text"],v["verbose"])
else:
self.observer.updatevar(k, v)
# Request the BayesOpt Process for the posterior of the given model
def queryposterior(self, xdata, modelid):
self.a.send({"posterior": xdata, "modelid": modelid})
# Finish BayesOpt
def endbayesopt(self):
self.a.send({"stop": True, "stop_posterior_connector": True})
self.console.log("Attempting to stop the Bayesian Optimization")
self.console.log("Please Wait...")
self.console.log("Saving the data...")
# Clear up the processes and close the pipes
def closestage(self):
self.b.send({"stop_connector": True})
self.endbayesopt()
self.a.close()
self.b.close()
self.p.terminate()