本文整理汇总了Python中multiprocessing.connection.Connection.close方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.close方法的具体用法?Python Connection.close怎么用?Python Connection.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.connection.Connection
的用法示例。
在下文中一共展示了Connection.close方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RawSocketHandler
# 需要导入模块: from multiprocessing.connection import Connection [as 别名]
# 或者: from multiprocessing.connection.Connection import close [as 别名]
class RawSocketHandler(socketserver.BaseRequestHandler):
def setup(self):
registry.append(self)
def handle(self):
self.conn = Connection(self.request.detach())
while self.conn._handle:
try:
obj = json.loads(self.conn.recv_bytes().decode('utf-8'))
except EOFError:
break
if obj['type'] == 'message':
bus.post(nt_from_dict(Message, obj['message'], None))
self.conn.send_bytes(json.dumps({'ret': True}).encode('utf-8'))
elif obj['type'] == 'request':
m = bus.post_sync(nt_from_dict(Message, obj['message'], None))
if m:
ret = {"ret": True, "response": m._asdict()}
else:
ret = {"ret": False, "response": None}
self.conn.send_bytes(json.dumps(ret).encode('utf-8'))
def send(self, msg):
if isinstance(msg, Message):
ret = {"type": "message", "message": msg._asdict()}
else:
ret = {"type": "response", "response": msg._asdict()}
self.conn.send_bytes(json.dumps(ret).encode('utf-8'))
def finish(self):
registry.remove(self)
def close(self):
self.conn.close()
示例2: device_send
# 需要导入模块: from multiprocessing.connection import Connection [as 别名]
# 或者: from multiprocessing.connection.Connection import close [as 别名]
def device_send(cls, ctrl_connection: Connection, send_config: SendConfig, dev_parameters: OrderedDict):
if not cls.init_device(ctrl_connection, is_tx=True, parameters=dev_parameters):
ctrl_connection.send("failed to start tx mode")
return False
if cls.ASYNCHRONOUS:
ret = cls.enter_async_send_mode(send_config.get_data_to_send)
else:
ret = cls.prepare_sync_send(ctrl_connection)
if ret != 0:
ctrl_connection.send("failed to start tx mode")
return False
exit_requested = False
buffer_size = cls.CONTINUOUS_TX_CHUNK_SIZE if send_config.continuous else cls.SYNC_TX_CHUNK_SIZE
if not cls.ASYNCHRONOUS and buffer_size == 0:
logger.warning("Send buffer size is zero!")
ctrl_connection.send("successfully started tx mode")
while not exit_requested and not send_config.sending_is_finished():
if cls.ASYNCHRONOUS:
try:
time.sleep(0.5)
except KeyboardInterrupt:
pass
else:
cls.send_sync(send_config.get_data_to_send(buffer_size))
while ctrl_connection.poll():
result = cls.process_command(ctrl_connection.recv(), ctrl_connection, is_tx=True)
if result == cls.Command.STOP.name:
exit_requested = True
break
if not cls.ASYNCHRONOUS:
# Some Sync send calls (e.g. USRP) are not blocking, so we wait a bit here to ensure
# that the send buffer on the SDR is cleared
time.sleep(0.75)
if exit_requested:
logger.debug("{}: exit requested. Stopping sending".format(cls.__class__.__name__))
if send_config.sending_is_finished():
logger.debug("{}: sending is finished.".format(cls.__class__.__name__))
cls.shutdown_device(ctrl_connection, is_tx=True)
ctrl_connection.close()
示例3: device_receive
# 需要导入模块: from multiprocessing.connection import Connection [as 别名]
# 或者: from multiprocessing.connection.Connection import close [as 别名]
def device_receive(cls, data_connection: Connection, ctrl_connection: Connection, dev_parameters: OrderedDict):
if not cls.init_device(ctrl_connection, is_tx=False, parameters=dev_parameters):
ctrl_connection.send("failed to start rx mode")
return False
try:
cls.adapt_num_read_samples_to_sample_rate(dev_parameters[cls.Command.SET_SAMPLE_RATE.name])
except NotImplementedError:
# Many SDRs like HackRF or AirSpy do not need to calculate SYNC_RX_CHUNK_SIZE
# as default values are either fine or given by the hardware
pass
if cls.ASYNCHRONOUS:
ret = cls.enter_async_receive_mode(data_connection, ctrl_connection)
else:
ret = cls.prepare_sync_receive(ctrl_connection)
if ret != 0:
ctrl_connection.send("failed to start rx mode")
return False
exit_requested = False
ctrl_connection.send("successfully started rx mode")
while not exit_requested:
if cls.ASYNCHRONOUS:
try:
time.sleep(0.25)
except KeyboardInterrupt:
pass
else:
cls.receive_sync(data_connection)
while ctrl_connection.poll():
result = cls.process_command(ctrl_connection.recv(), ctrl_connection, is_tx=False)
if result == cls.Command.STOP.name:
exit_requested = True
break
cls.shutdown_device(ctrl_connection, is_tx=False)
data_connection.close()
ctrl_connection.close()
示例4: _handle
# 需要导入模块: from multiprocessing.connection import Connection [as 别名]
# 或者: from multiprocessing.connection.Connection import close [as 别名]
def _handle(self, conn_lru_dict: LRUCacheType[multiprocessing_connection.Connection, bool],
conn: multiprocessing_connection.Connection, c_send: multiprocessing_connection.Connection):
try:
data = conn.recv_bytes()
if not data:
raise EOFError
self.logger.debug("parse conn %s" % conn)
# self.logger.debug(data)
try:
result = self.handle(data)
if result is not None:
conn.send_bytes(result)
except Exception:
self.logger.exception("handle error")
conn_lru_dict[conn] = True
c_send.send_bytes(b'ok')
except OSError:
self.logger.debug("conn %s was closed" % conn)
conn.close()
except EOFError:
self.logger.debug("conn %s was eof" % conn)
conn.close()
except BrokenPipeError:
self.logger.debug("conn %s was broken" % conn)
conn.close()