0

I have code like this :

zmq = Zmq_Connector_Mod.DWX_ZeroMQ_Connector()
zmq._GET_HIST_INDICATORS_(_symbol, 'C1')
sleep(random() * 5 )
c1_path = zmq._GET_DATA_()

zmq = Zmq_Connector_Mod.DWX_ZeroMQ_Connector()
zmq._GET_HIST_INDICATORS_(_symbol, 'BASELINE')
sleep(random() * 5 )
baseline_path = zmq._GET_DATA_()

zmq = Zmq_Connector_Mod.DWX_ZeroMQ_Connector()
zmq._GET_HIST_INDICATORS_(_symbol, 'C2')
sleep(random() * 5 )
c2_path = zmq._GET_DATA_()

zmq = Zmq_Connector_Mod.DWX_ZeroMQ_Connector()
zmq._GET_HIST_INDICATORS_(_symbol, 'EXIT')
sleep(random() * 5 )
exit_path = zmq._GET_DATA_()

I have a problem when zmq._GET_DATA_() is running, it doesn't have returned value, because zmq._GET_HIST_INDICATORS_() function needs a couple seconds to return the value. I already used sleep(), but it's not efficient because when I try to run this code in another device that slower than mine, it just not helping. How to wait program from execute the zmq._GET_DATA_() until zmq._GET_HIST_INDICATORS_() has returned the value without using sleep() that need time, meanwhile every device has different running time to execute the code ?

Selcuk
  • 57,004
  • 12
  • 102
  • 110
Agam Theos
  • 17
  • 6

2 Answers2

1

Higher-level overview here: typically in asynchronous message queueing, there are a few patterns you can use so you don't have to poll over and over:

  1. Publish-subscribe
  2. Get with wait
  3. Request-reply
  4. Message listener

  1. This is implementable in ZeroMQ, e.g. https://rillabs.org/posts/pub-sub-with-zeromq-in-python and this stackoverflow question discusses it in detail: ZeroMQ - Multiple Publishers and Listener

  2. Get with wait is a pattern where a timeout is set for a get operation, it won't return an error until the time expires. On a typical zmq.recv() call, you can specify the timeout.

  3. Request-reply is typically implemented where the requestor specifies a reply queue and does a get with wait operation. Using this means you'll know which returned message corresponds to each message you sent. https://zguide.zeromq.org/docs/chapter3/#Recap-of-Request-Reply-Sockets

  4. Message listeners set up responsive objects that respond to events and can be implemented in various ways. Various message queueing technologies have this built-in, couldn't find a good zmq example but it's definitely implementable!


Other queueing technologies have these patterns implemented more readily, e.g. ActiveMQ, IBM MQ, RabbitMQ if you wanted to explore.

MaxK
  • 11
  • 4
0

It looks like you are using a message queue, so there must be a documented async way of doing this, but you may try something like the following:

exit_path = None
while exit_path is None:
    try:
        exit_path = zmq._GET_DATA_()
    except AttributeError:
        exit_path = None
        sleep(1)

This should check once every second to see if the data is available.

Selcuk
  • 57,004
  • 12
  • 102
  • 110