问题
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
.
When it comes to grabbing and printing the data, I just cannot make it happen.
This is my push instance which works just fine:
Metatrader 4 zeroMQ push instance:
#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:
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("\nResource timeout.. please try again.")
sleep(0.000001)
return None
Console output without printed data :-(
回答1:
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.
You should use this code directly in loop (without define function and without return
)
while True:
try:
msg = zmq_socket.recv_string()
print(msg)
except zmq.error.Again:
print("\nResource 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("\nResource timeout.. please try again.")
sleep(0.000001)
return None
while True:
pull_MT4() # execute function
来源:https://stackoverflow.com/questions/59127530/i-cant-grab-print-data-on-the-pull-instance-using-plain-zeromq-push-pull-patter