Example #1
0
def signal_wakeup(app):
    """
    Allow to be notified in python for signals when in long-running calls from
    the C or c++ side, like QApplication.exec_().

    See https://stackoverflow.com/a/37229299.
    """
    sock = QAbstractSocket(QAbstractSocket.UdpSocket, app)
    # Create a socket pair
    sock.wsock, sock.rsock = socket.socketpair(type=socket.SOCK_DGRAM)
    # Let Qt listen on the one end
    sock.setSocketDescriptor(sock.rsock.fileno())
    # And let Python write on the other end
    sock.wsock.setblocking(False)
    signal.set_wakeup_fd(sock.wsock.fileno())
    # add a dummy callback just to be on the python side as soon as possible.
    sock.readyRead.connect(lambda: None)
Example #2
0
def disable_nagle(socket: QAbstractSocket) -> None:
    # http://doc.qt.io/qt-5/qabstractsocket.html#SocketOption-enum
    socket.setSocketOption(QAbstractSocket.LowDelayOption, 1)
Example #3
0
def get_socket_error(socket: QAbstractSocket) -> str:
    return "{}: {}".format(socket.error(), socket.errorString())
Example #4
0
def is_socket_connected(socket: QAbstractSocket) -> bool:
    return socket and socket.state() == QAbstractSocket.ConnectedState