Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def subscriber_listener():
subscriber = zmq_ctx.socket(zmq.SUB)
subscriber.connect("tcp://%s:5555" % director_ip)
subscriber.setsockopt(zmq.SUBSCRIBE, "")
print "subscriber_listener", director_ip
while True:
try:
print "subscriber_listen"
msg = subscriber.recv()
print msg
task = json.loads(msg)
task_type = task.get("task")
if task_type == "deploy":
docker_tasks.put_nowait(task) # just send the task dict
elif task_type == "undeploy":
docker_tasks.put_nowait(task) # just send the task dict
except Exception as e:
print "subscriber_listener", e
traceback.print_exc(file=sys.stdout)
def __init__(self, container, endpoint=None):
self.container = container
self.stats = None
self.endpoint = endpoint or DEFAULT_MONITOR_ENDPOINT
logger.info('connecting to monitor endpoint %s', self.endpoint)
ctx = zmq.Context.instance()
self.socket = ctx.socket(zmq.PUB)
self.socket.connect(self.endpoint)
self.fqdn = socket.getfqdn()
def _distribute(self, peer, topic, headers, message=None, bus='',
required_capabilities=[]):
subscriptions = self._peer_subscriptions[bus]
subscribers = set()
for prefix, subscription in subscriptions.iteritems():
if subscription and topic.startswith(prefix):
subscribers |= subscription
if subscribers:
sender = encode_peer(peer)
json_msg = jsonapi.dumps(jsonrpc.json_method(
None, 'pubsub.push',
[sender, bus, topic, headers, message], None))
frames = [zmq.Frame(b''), zmq.Frame(b''),
zmq.Frame(b'RPC'), zmq.Frame(json_msg)]
socket = self.core().socket
for subscriber in subscribers:
socket.send(subscriber, flags=SNDMORE)
socket.send_multipart(frames, copy=False)
return len(subscribers)
#!/usr/bin/env python2
from itertools import takewhile
import gevent
from gevent import spawn, queue, event
import zmq.green as zmq
import cPickle as pickle
import _importhack
from cuburn.render import Renderer
from messages import *
ctx = zmq.Context()
def setup_task_listeners(addrs, tq, rq):
hisock = ctx.socket(zmq.REP)
losock = ctx.socket(zmq.REP)
hisock.bind(addrs['tasks'])
losock.bind(addrs['tasks_loprio'])
loevt = event.Event()
loevt.set()
@spawn
def listen_hi():
while True:
if not hisock.poll(timeout=0):
# No messages pending. Set loevt, allowing messages from
# losock to be added to the queue.
# }}}
from __future__ import print_function, absolute_import
import logging
import logging.config
import os
import re
import zmq
from zmq import SNDMORE, EHOSTUNREACH, ZMQError, EAGAIN, NOBLOCK
from zmq import green
from collections import defaultdict
# Create a context common to the green and non-green zmq modules.
green.Context._instance = green.Context.shadow(zmq.Context.instance().underlying)
from .agent.subsystems.pubsub import ProtectedPubSubTopics
from volttron.platform.jsonrpc import (INVALID_REQUEST, UNAUTHORIZED)
from volttron.platform.vip.agent.errors import VIPError
from volttron.platform.agent import json as jsonapi
# Optimizing by pre-creating frames
_ROUTE_ERRORS = {
errnum: (zmq.Frame(str(errnum).encode('ascii')),
zmq.Frame(os.strerror(errnum).encode('ascii')))
for errnum in [zmq.EHOSTUNREACH, zmq.EAGAIN]
}
class PubSubService(object):
def __init__(self, socket, protected_topics, routing_service, *args, **kwargs):
self._logger = logging.getLogger(__name__)
# return URL on ZMQ channel if specified in extra_prms
if 'zmq_impath_return_ch' in extra_prms:
log.info('Returning image URL on ZMQ channel: %s', extra_prms['zmq_impath_return_ch'])
created_sock = True
try:
# either reuse or create new zmq socket
if 'zmq_impath_return_sock' not in extra_prms:
# either reuse or create new zmq context
# (to create socket)
if 'zmq_context' in extra_prms:
log.info('Reusing ZMQ_CONTEXT')
context = extra_prms['zmq_context']
else:
log.info('Generating new ZMQ_CONTEXT')
context = zmq.Context()
impath_sender = context.socket(zmq.REQ)
impath_sender.connect(extra_prms['zmq_impath_return_ch'])
else:
created_sock = False
log.info('Reusing ZMQ_SOCKET')
impath_sender = extra_prms['zmq_impath_return_sock']
impath_sender.send(str(out_dict['clean_fn']))
impath_sender.recv()
log.info('Completed request')
finally:
if created_sock: impath_sender.close()
else:
log.info('Not returning image URL over ZMQ channel (not specified)')
def send_zmq_push(actor_url, data):
context = shared.zmq_context
socket = context.socket(zmq.PUSH)
socket.connect(actor_url)
socket.send(data)
socket.close()
gevent.sleep()
def __init__(self, sleep_interval=3, pool_size=8):
self.context = zmq.Context()
self.sockets = SocketManager(zmq, self.context)
self.sleep_interval = sleep_interval
self.pool_size = pool_size
self.pool = Pool(pool_size)
self.allowed_to_run = Event()
self.allowed_to_run.set()
def __init__(self, name, pid):
self.name = name
self.pid = pid
self.ctx = zmq.Context()
spawn_greenlet(self.set_up_sockets)
self.keep_running = True
self.logger = get_logger_for_class(self.__class__)
self.log_connected()
def _sending(self, flags):
flags |= getattr(self._Socket__local, 'flags', 0)
lock = self._Socket__send_lock
while not lock.acquire(not flags & NOBLOCK):
if not self.poll(0, POLLOUT):
raise _green.Again()
_sleep(0)
try:
yield flags
finally:
lock.release()
class BaseRouter(_BaseRouter):
_context_class = _green.Context
_socket_class = _green.Socket