1

我无法在 PUSH/PULL 模式中的 nanomsg python 上正确设置超时。我使用 set socket 选项函数并传入一个 ctype 对象。两个 set_sock_options 都返回成功。我错过了什么吗?

套接字用于接收和发送,但有无限超时。当我使用 get_sock_option 它返回我刚刚设置的值,所以我的输入似乎有某种效果。

NN_RCVTIMEO = 5
NN_SNDTIMEO = 4
NN_SOL_SOCKET = 0

message = ""

timeout = ctypes.create_string_buffer(b'500');

#Bind input socket
socket_in = nn_wrapper.nn_socket(AF_SP, PULL)
sucess = nn_wrapper.nn_setsockopt(socket_in, NN_SOL_SOCKET, NN_RCVTIMEO, timeout)
nn_wrapper.nn_bind(socket_in, 'tcp://127.0.0.1:64411')
time.sleep(0.2)

print("SUCESS?" + str(sucess))


#Send inquiry
socket_out = nn_wrapper.nn_socket(AF_SP, PUSH)
sucess = nn_wrapper.nn_setsockopt(socket_out, NN_SOL_SOCKET, NN_SNDTIMEO, timeout)
nn_wrapper.nn_connect(socket_out, 'tcp://127.0.0.1:64400')
time.sleep(0.2)

print("SUCESS?" + str(sucess))

nn_wrapper.nn_send(socket_out, b'HELLO',0)

#Received...
bytes, message = nn_wrapper.nn_recv(socket_in, 0)

nn_wrapper.nn_close(socket_in)
nn_wrapper.nn_close(socket_out)
4

2 回答 2

1

迟到了,希望能帮助下一个人……

库的作者已经想到了这些问题,并set_int_option为socket对象提供了一个python。

这对我有用:

import nanomsg


class ManagementClient:
    def __init__(self, endpoint):
        self._client_socket = nanomsg.Socket(nanomsg.REQ)
        self._client_socket.set_int_option(nanomsg.SOL_SOCKET, nanomsg.RCVTIMEO, 5 * 1000)
        self._client_socket.connect("tcp://" + endpoint)

于 2019-05-25T11:26:38.317 回答
1

看起来当使用通过引用(在本例中为整数)接收参数的 C 函数包装器时,您必须使用“结构”模块。这就是使用整数值500作为nn_setsockopt()函数的参数的方式:

from struct import Struct
from nanomsg import wrapper as nn_wrapper
from nanomsg import Socket, PULL, PUSH, AF_SP
import ctypes

NN_RCVTIMEO = 5
NN_SOL_SOCKET = 0

INT_PACKER = Struct(str('i'))
timeout = (ctypes.c_ubyte * INT_PACKER.size)()
INT_PACKER.pack_into(timeout, 0, 500)

socket_in = nn_wrapper.nn_socket(AF_SP, PULL)
nn_wrapper.nn_bind(socket_in, 'tcp:///127.0.0.1:60000')
nn_wrapper.nn_setsockopt(socket_in, NN_SOL_SOCKET, NN_RCVTIMEO, timeout)
于 2018-01-17T13:27:12.453 回答