Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

python - 我无法使用纯零MQ推/拉模式在拉取实例上获取/打印数据(I can't grab/print data on the pull instance using plain zeroMQ Push/Pull pattern)

I am having tough times to grab and print data which is successfully pushed to the wire.

(我在获取和打印成功推送到数据的数据时遇到困难。)

I have set up a plain Push-Pull-Architecture with Metatrader 4 acting as producer and Python backend acting as consumer .

(我已经建立了一个简单的推挽式架构,其中Metatrader 4充当了producer ,Python后端充当了consumer 。)

When it comes to grabbing and printing the data, I just cannot make it happen.

(在获取和打印数据时,我只是无法实现。)

This is my push instance which works just fine:

(这是我的push实例,效果很好:)

Metatrader 4 zeroMQ push instance:

(Metatrader 4 zeroMQ推送实例:)

#include <Zmq/Zmq.mqh>

// EA plot settings

extern string PROJECT_NAME = "Dashex.Feeder";
extern string ZEROMQ_PROTOCOL = "tcp";
extern string HOSTNAME = "localhost";
extern int PUSH_PORT = 32225;

extern string t0 = "--- Feeder Parameters ---";
input string DID = "insert your DID here";
extern string t1 = "--- ZeroMQ Configuration ---";
extern bool Publish_MarketData = false;

// ZeroMQ environment //

// CREATE ZeroMQ Context
Context context(PROJECT_NAME);

// CREATE ZMQ_PUSH SOCKET
Socket pushSocket(context, ZMQ_PUSH);

string Publish_Symbols[7] = {
   "EURUSD","GBPUSD","USDJPY","USDCAD","AUDUSD","NZDUSD","USDCHF"
};

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int OnInit()
  {
//---

   EventSetTimer(1);     // Set Millisecond Timer to get client socket input

   context.setBlocky(false);

   // Send responses to PULL_PORT that client is listening on.
   Print("[PUSH] Connecting MT4 Server to Socket on Port " + IntegerToString(PUSH_PORT) + "..");
   pushSocket.connect(StringFormat("%s://%s:%d", ZEROMQ_PROTOCOL, HOSTNAME, PUSH_PORT));

   pushSocket.setSendHighWaterMark(1);
   pushSocket.setLinger(0);   

//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---

   Print("[PUSH] Disconnecting MT4 Server from Socket on Port " + IntegerToString(PUSH_PORT) + "..");
   pushSocket.disconnect(StringFormat("%s://%s:%d", ZEROMQ_PROTOCOL, HOSTNAME, PUSH_PORT));

   // Shutdown ZeroMQ Context
   context.shutdown();
   context.destroy(0);

   EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTimer()
{
   /*
      Use this OnTimer() function to send market data to consumer.
   */
   if(!IsStopped() && Publish_MarketData == true)
   {
      for(int s = 0; s < ArraySize(Publish_Symbols); s++)
      {

         string _tick = GetBidAsk(Publish_Symbols[s]);
         Print("Sending " + Publish_Symbols[s] + " " + _tick + " to PUSH Socket");
         ZmqMsg reply(StringFormat("%s %s", Publish_Symbols[s], _tick));
         pushSocket.send(reply, true);
      }
   }
}
//+------------------------------------------------------------------+

string GetBidAsk(string symbol) {

   MqlTick last_tick;

   if(SymbolInfoTick(symbol,last_tick))
   {
       return(StringFormat("%f;%f", last_tick.bid, last_tick.ask));
   }

   // Default
   return "";
}

How data is pushed to the wire (array of strings, snippet from above code):

(如何将数据推送到线路(上述代码中的字符串数组,代码段):)

         string _tick = GetBidAsk(Publish_Symbols[s]);
         Print("Sending " + Publish_Symbols[s] + " " + _tick + " to PUSH Socket");
         ZmqMsg reply(StringFormat("%s %s", Publish_Symbols[s], _tick));
         pushSocket.send(reply, true);

在此处输入图片说明

Python side pull client which doesn't grab/print any data from the wire:

(Python侧拉客户端不会从网络中获取/打印任何数据:)

import zmq
import time
from time import sleep

context = zmq.Context()
zmq_socket = context.socket(zmq.PULL)
zmq_socket.bind("tcp://*:32220")
time.sleep(1)

while True:

    def pull_MT4():

        try:
            msg = zmq_socket.recv_string()
            print(msg)
            return msg
        except zmq.error.Again:
            print("
Resource timeout.. please try again.")
            sleep(0.000001)

        return None

Console output without printed data :-(

(控制台输出,无打印数据:-()

在此处输入图片说明

  ask by Phanti translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Inside while True you only define function pull_MT4 again and again but you never execute this function - you never use pull_MT4() to run it.

(在while True您只一次又一次定义函数pull_MT4 ,但从未执行此函数-从未使用过pull_MT4()来运行它。)

You should use this code directly in loop (without define function and without return )

(您应该直接在循环中使用此代码(没有define函数,没有return ))

while True:
    try:
        msg = zmq_socket.recv_string()
        print(msg)
    except zmq.error.Again:
        print("
Resource timeout.. please try again.")
        sleep(0.000001)

or you should define function before loop and execute it inside loop

(或者您应该在循环之前定义函数并在循环内执行)

def pull_MT4(): # define function
    try:
        msg = zmq_socket.recv_string()
        print(msg)
        return msg
    except zmq.error.Again:
        print("
Resource timeout.. please try again.")
        sleep(0.000001)
    return None

while True:
    pull_MT4() # execute function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...