I am working on zeromq PUB/SUB and I setuped zeromq PUB on django.
我正在研究zeromq PUB / SUB,我在django上设置了zeromq PUB。
This is my code that i use for setting up zeromq
这是我用来设置zeromq的代码
from gevent_zeromq import zmq
i am using gevent_zeromq library
我正在使用gevent_zeromq库
@api_view(['POST','GET'])
def NOTIFICATION(request):
if request.method == 'GET':
return render_to_response('notifications.html',context)
if request.method == 'POST':
message_json = json.dumps(request.data)
message_load = json.loads(message_json)
message = {message_load['msg']}
ctx = zmq.Context()
publisher = ctx.socket(zmq.PUB)
publisher.bind("tcp://*:5566")
time.sleep(1)
while True:
publisher.send_multipart("message:%s" % str(message))
time.sleep(1)
publisher.close()
ctx.term()
return Response('',status=200)
I am using ajax/jquery for data coming throgh web.
我正在使用ajax / jquery来处理来自throgh web的数据。
This code is working, but when I send another message from frontend it is giving me error:
这段代码正常,但是当我从前端发送另一条消息时,它给了我错误:
File "/home/admin/dx_man/dx_man/views.py", line 42, in NOTIFICATION
publisher.bind("tcp://*:5566")
File "socket.pyx", line 447, in zmq.core.socket.Socket.bind (zmq/core/socket.c:4312)
ZMQError: Address already in use
How can I do it with using ajax, django.zeromq.
我怎么能用ajax,django.zeromq做到这一点。
I want to make only publisher, subscriber will be seperate so once publisher will publish the message later on subscriber will be handle it, but here I am facing issue.
我想只制作发布者,订阅者将是单独的,所以一旦发布者将在稍后发布消息,订阅者将处理它,但在这里我面临问题。
Can anyone give me best solution for it so that I can handle all message from web interface and I also want to store messages into the database, that I will handle once message will be publish.
任何人都可以给我最好的解决方案,以便我可以处理来自Web界面的所有消息,我也想将消息存储到数据库中,一旦消息将被发布,我将处理。
1 个解决方案
#1
4
Since you're spinning up the ZMQ context and socket for each request, if anything prevents the socket and context from terminating and releasing the port before your next message then that will cause an issue when you try to bind on the same port again.
由于您为每个请求启动了ZMQ上下文和套接字,如果有任何事情阻止套接字和上下文在您的下一条消息之前终止并释放端口,那么当您尝试再次绑定到同一端口时,这将导致问题。
You have a couple of options:
你有几个选择:
- Create the context and socket once, and re-use them for each request (this would be my preferred approach, so long as the Python process is persistent across calls, which it may not be)
- 创建一次上下文和套接字,并为每个请求重用它们(这是我的首选方法,只要Python进程在调用之间是持久的,它可能不是)
- remove the
time.sleep(1)
calls, as they might be causing the process to take too long to close - 删除time.sleep(1)调用,因为它们可能导致进程花费太长时间才能关闭
- set ZMQ_LINGER to zero before you close, which will force it to close even if there are still messages waiting... which could mean some messages don't get sent, if they are taking too long.
- 在关闭之前将ZMQ_LINGER设置为零,这将迫使它关闭,即使仍有消息等待...这可能意味着某些消息不会被发送,如果它们花费的时间太长。
- If you absolutely need to spin up your context and socket each time to send your message, and they'll be coming fast enough that you can't break the last one down before the next one comes in, then you should be
connect()
-ing on your PUB socket andbind()
-ing on your SUB socket. - 如果你绝对需要每次都发送你的消息来启动你的上下文和套接字,并且它们将足够快到你不能在下一个进入之前打破最后一个,那么你应该是connect() - 在你的PUB套接字和bind() - 你的SUB套接字上。
This is a classic case where your PUB socket is temporary and your SUB socket is reliable, so your SUB socket should be your "server" and bind()
.
这是一个典型的情况,你的PUB套接字是临时的,你的SUB套接字是可靠的,所以你的SUB套接字应该是你的“服务器”和bind()。
#1
4
Since you're spinning up the ZMQ context and socket for each request, if anything prevents the socket and context from terminating and releasing the port before your next message then that will cause an issue when you try to bind on the same port again.
由于您为每个请求启动了ZMQ上下文和套接字,如果有任何事情阻止套接字和上下文在您的下一条消息之前终止并释放端口,那么当您尝试再次绑定到同一端口时,这将导致问题。
You have a couple of options:
你有几个选择:
- Create the context and socket once, and re-use them for each request (this would be my preferred approach, so long as the Python process is persistent across calls, which it may not be)
- 创建一次上下文和套接字,并为每个请求重用它们(这是我的首选方法,只要Python进程在调用之间是持久的,它可能不是)
- remove the
time.sleep(1)
calls, as they might be causing the process to take too long to close - 删除time.sleep(1)调用,因为它们可能导致进程花费太长时间才能关闭
- set ZMQ_LINGER to zero before you close, which will force it to close even if there are still messages waiting... which could mean some messages don't get sent, if they are taking too long.
- 在关闭之前将ZMQ_LINGER设置为零,这将迫使它关闭,即使仍有消息等待...这可能意味着某些消息不会被发送,如果它们花费的时间太长。
- If you absolutely need to spin up your context and socket each time to send your message, and they'll be coming fast enough that you can't break the last one down before the next one comes in, then you should be
connect()
-ing on your PUB socket andbind()
-ing on your SUB socket. - 如果你绝对需要每次都发送你的消息来启动你的上下文和套接字,并且它们将足够快到你不能在下一个进入之前打破最后一个,那么你应该是connect() - 在你的PUB套接字和bind() - 你的SUB套接字上。
This is a classic case where your PUB socket is temporary and your SUB socket is reliable, so your SUB socket should be your "server" and bind()
.
这是一个典型的情况,你的PUB套接字是临时的,你的SUB套接字是可靠的,所以你的SUB套接字应该是你的“服务器”和bind()。