当前位置: 首页>>代码示例>>Python>>正文


Python QLocalSocket.waitForDisconnected方法代码示例

本文整理汇总了Python中PyQt5.QtNetwork.QLocalSocket.waitForDisconnected方法的典型用法代码示例。如果您正苦于以下问题:Python QLocalSocket.waitForDisconnected方法的具体用法?Python QLocalSocket.waitForDisconnected怎么用?Python QLocalSocket.waitForDisconnected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PyQt5.QtNetwork.QLocalSocket的用法示例。


在下文中一共展示了QLocalSocket.waitForDisconnected方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _has_legacy_server

# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import waitForDisconnected [as 别名]
def _has_legacy_server(name):
    """Check if there is a legacy server.

    Args:
        name: The name to try to connect to.

    Return:
        True if there is a server with the given name, False otherwise.
    """
    socket = QLocalSocket()
    log.ipc.debug("Trying to connect to {}".format(name))
    socket.connectToServer(name)

    err = socket.error()

    if err != QLocalSocket.UnknownSocketError:
        log.ipc.debug("Socket error: {} ({})".format(socket.errorString(), err))

    os_x_fail = (
        sys.platform == "darwin" and socket.errorString() == "QLocalSocket::connectToServer: " "Unknown error 38"
    )

    if err not in [QLocalSocket.ServerNotFoundError, QLocalSocket.ConnectionRefusedError] and not os_x_fail:
        return True

    socket.disconnectFromServer()
    if socket.state() != QLocalSocket.UnconnectedState:
        socket.waitForDisconnected(CONNECT_TIMEOUT)
    return False
开发者ID:derlaft,项目名称:qutebrowser,代码行数:31,代码来源:ipc.py

示例2: send_to_running_instance

# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import waitForDisconnected [as 别名]
def send_to_running_instance(socketname, command, target_arg, *,
                             legacy_name=None, socket=None):
    """Try to send a commandline to a running instance.

    Blocks for CONNECT_TIMEOUT ms.

    Args:
        socketname: The name which should be used for the socket.
        command: The command to send to the running instance.
        target_arg: --target command line argument
        socket: The socket to read data from, or None.
        legacy_name: The legacy name to first try to connect to.

    Return:
        True if connecting was successful, False if no connection was made.
    """
    if socket is None:
        socket = QLocalSocket()

    if (legacy_name is not None and
            _has_legacy_server(legacy_name)):
        name_to_use = legacy_name
    else:
        name_to_use = socketname

    log.ipc.debug("Connecting to {}".format(name_to_use))
    socket.connectToServer(name_to_use)

    connected = socket.waitForConnected(CONNECT_TIMEOUT)
    if connected:
        log.ipc.info("Opening in existing instance")
        json_data = {'args': command, 'target_arg': target_arg,
                     'version': qutebrowser.__version__,
                     'protocol_version': PROTOCOL_VERSION}
        try:
            cwd = os.getcwd()
        except OSError:
            pass
        else:
            json_data['cwd'] = cwd
        line = json.dumps(json_data) + '\n'
        data = line.encode('utf-8')
        log.ipc.debug("Writing: {}".format(data))
        socket.writeData(data)
        socket.waitForBytesWritten(WRITE_TIMEOUT)
        if socket.error() != QLocalSocket.UnknownSocketError:
            raise SocketError("writing to running instance", socket)
        else:
            socket.disconnectFromServer()
            if socket.state() != QLocalSocket.UnconnectedState:
                socket.waitForDisconnected(CONNECT_TIMEOUT)
            return True
    else:
        if socket.error() not in (QLocalSocket.ConnectionRefusedError,
                                  QLocalSocket.ServerNotFoundError):
            raise SocketError("connecting to running instance", socket)
        else:
            log.ipc.debug("No existing instance present (error {})".format(
                socket.error()))
            return False
开发者ID:NicoloPernigo,项目名称:qutebrowser,代码行数:62,代码来源:ipc.py

示例3: _send_to_running_instance

# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import waitForDisconnected [as 别名]
    def _send_to_running_instance(self, payload: bytes, pid: int) -> None:
        from PyQt5.QtCore import QByteArray
        from PyQt5.QtNetwork import QLocalSocket

        named_pipe = f"{BUNDLE_IDENTIFIER}.protocol.{pid}"
        log.debug(
            f"Opening a local socket to the running instance on {named_pipe} "
            f"(payload={self.redact_payload(payload)})"
        )
        client = QLocalSocket()
        try:
            client.connectToServer(named_pipe)

            if not client.waitForConnected():
                log.error(f"Unable to open client socket: {client.errorString()}")
                return

            client.write(QByteArray(payload))
            client.waitForBytesWritten()
            client.disconnectFromServer()
            if client.state() == QLocalSocket.ConnectedState:
                client.waitForDisconnected()
        finally:
            del client
        log.debug("Successfully closed client socket")
开发者ID:nuxeo,项目名称:nuxeo-drive,代码行数:27,代码来源:commandline.py

示例4: send_to_running_instance

# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import waitForDisconnected [as 别名]
def send_to_running_instance(socketname, command):
    """Try to send a commandline to a running instance.

    Blocks for CONNECT_TIMEOUT ms.

    Args:
        socketname: The name which should be used for the socket.
        command: The command to send to the running instance.

    Return:
        True if connecting was successful, False if no connection was made.
    """
    socket = QLocalSocket()
    log.ipc.debug("Connecting to {}".format(socketname))
    socket.connectToServer(socketname)
    connected = socket.waitForConnected(100)
    if connected:
        log.ipc.info("Opening in existing instance")
        json_data = {'args': command}
        try:
            cwd = os.getcwd()
        except OSError:
            pass
        else:
            json_data['cwd'] = cwd
        line = json.dumps(json_data) + '\n'
        data = line.encode('utf-8')
        log.ipc.debug("Writing: {}".format(data))
        socket.writeData(data)
        socket.waitForBytesWritten(WRITE_TIMEOUT)
        if socket.error() != QLocalSocket.UnknownSocketError:
            _socket_error("writing to running instance", socket)
        else:
            socket.disconnectFromServer()
            if socket.state() != QLocalSocket.UnconnectedState:
                socket.waitForDisconnected(100)
            return True
    else:
        if socket.error() not in (QLocalSocket.ConnectionRefusedError,
                                  QLocalSocket.ServerNotFoundError):
            _socket_error("connecting to running instance", socket)
        else:
            log.ipc.debug("No existing instance present (error {})".format(
                socket.error()))
            return False
开发者ID:jnphilipp,项目名称:qutebrowser,代码行数:47,代码来源:ipc.py

示例5: startClient

# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import waitForDisconnected [as 别名]
    def startClient(self) -> bool:
        Logger.log("i", "Checking for the presence of an ready running Cura instance.")
        single_instance_socket = QLocalSocket(self._application)
        Logger.log("d", "Full single instance server name: %s", single_instance_socket.fullServerName())
        single_instance_socket.connectToServer("ultimaker-cura")
        single_instance_socket.waitForConnected(msecs = 3000)  # wait for 3 seconds

        if single_instance_socket.state() != QLocalSocket.ConnectedState:
            return False

        # We only send the files that need to be opened.
        if not self._files_to_open:
            Logger.log("i", "No file need to be opened, do nothing.")
            return True

        if single_instance_socket.state() == QLocalSocket.ConnectedState:
            Logger.log("i", "Connection has been made to the single-instance Cura socket.")

            # Protocol is one line of JSON terminated with a carriage return.
            # "command" field is required and holds the name of the command to execute.
            # Other fields depend on the command.

            payload = {"command": "clear-all"}
            single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii"))

            payload = {"command": "focus"}
            single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii"))

            for filename in self._files_to_open:
                payload = {"command": "open", "filePath": os.path.abspath(filename)}
                single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii"))

            payload = {"command": "close-connection"}
            single_instance_socket.write(bytes(json.dumps(payload) + "\n", encoding = "ascii"))

            single_instance_socket.flush()
            single_instance_socket.waitForDisconnected()
        return True
开发者ID:TinkerGnome,项目名称:Cura,代码行数:40,代码来源:SingleInstance.py

示例6: qlocalsocket

# 需要导入模块: from PyQt5.QtNetwork import QLocalSocket [as 别名]
# 或者: from PyQt5.QtNetwork.QLocalSocket import waitForDisconnected [as 别名]
def qlocalsocket(qapp):
    socket = QLocalSocket()
    yield socket
    socket.disconnectFromServer()
    if socket.state() != QLocalSocket.UnconnectedState:
        socket.waitForDisconnected(1000)
开发者ID:axs221,项目名称:qutebrowser,代码行数:8,代码来源:test_ipc.py


注:本文中的PyQt5.QtNetwork.QLocalSocket.waitForDisconnected方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。