EventLoop
Listens for events and returns when any WebSocket has readable data, or when concurrent tasks such as exchange.Go() or HttpQuery_Go() complete.
EventLoop()
EventLoop(timeout)Examples
javascript
function main() {
var routine_getTicker = exchange.Go("GetTicker")
var routine_getDepth = exchange.Go("GetDepth")
var routine_getTrades = exchange.Go("GetTrades")
// Sleep(2000), if a Sleep statement is used here, it will cause the subsequent EventLoop function to miss the previous events. Because after waiting 2 seconds, the concurrent functions have already received data, and only then does the EventLoop listening mechanism start, so these events will be missed
// Unless EventLoop(-1) is called on the very first line to initialize the EventLoop listening mechanism first, these events will not be missed
// Log("GetDepth:", routine_getDepth.wait()) If the wait function is called here in advance to retrieve the result of the concurrent GetDepth function call, the event of this GetDepth function receiving the request result will not be returned in the EventLoop function
var ts1 = new Date().getTime()
var ret1 = EventLoop(0)
var ts2 = new Date().getTime()
var ret2 = EventLoop(0)
var ts3 = new Date().getTime()
var ret3 = EventLoop(0)
Log("First concurrent task completed:", _D(ts1), ret1)
Log("Second concurrent task completed:", _D(ts2), ret2)
Log("Third concurrent task completed:", _D(ts3), ret3)
Log("GetTicker:", routine_getTicker.wait())
Log("GetDepth:", routine_getDepth.wait())
Log("GetTrades:", routine_getTrades.wait())
}
python
import time
def main():
routine_getTicker = exchange.Go("GetTicker")
routine_getDepth = exchange.Go("GetDepth")
routine_getTrades = exchange.Go("GetTrades")
ts1 = time.time()
ret1 = EventLoop(0)
ts2 = time.time()
ret2 = EventLoop(0)
ts3 = time.time()
ret3 = EventLoop(0)
Log("First concurrent task completed:", _D(ts1), ret1)
Log("Second concurrent task completed:", _D(ts2), ret2)
Log("Third concurrent task completed:", _D(ts3), ret3)
Log("GetTicker:", routine_getTicker.wait())
Log("GetDepth:", routine_getDepth.wait())
Log("GetTrades:", routine_getTrades.wait())
rust
fn main() {
// In Rust, exchange.Go uses typed tokens (such as Go::GetTicker) instead of method-name strings; pass () when there are no arguments
let routine_getTicker = exchange.Go(Go::GetTicker, ());
let routine_getDepth = exchange.Go(Go::GetDepth, ());
let routine_getTrades = exchange.Go(Go::GetTrades, ());
// Sleep(2000), if a Sleep statement is used here, it will cause the subsequent EventLoop function to miss the previous events. Because after waiting 2 seconds, the concurrent functions have already received data, and only then does the EventLoop listening mechanism start, so these events will be missed
// Unless EventLoop(-1) is called on the very first line to initialize the EventLoop listening mechanism first, these events will not be missed
// Log!("GetDepth:", routine_getDepth.wait(0)) If the wait function is called here in advance to retrieve the result of the concurrent GetDepth function call, the event of this GetDepth function receiving the request result will not be returned in the EventLoop function
let ts1 = Unix() * 1000;
let ret1 = EventLoop(0);
let ts2 = Unix() * 1000;
let ret2 = EventLoop(0);
let ts3 = Unix() * 1000;
let ret3 = EventLoop(0);
Log!("First concurrent task completed:", _D(ts1), ret1);
Log!("Second concurrent task completed:", _D(ts2), ret2);
Log!("Third concurrent task completed:", _D(ts3), ret3);
Log!("GetTicker:", routine_getTicker.wait(0).unwrap());
Log!("GetDepth:", routine_getDepth.wait(0).unwrap());
Log!("GetTrades:", routine_getTrades.wait(0).unwrap());
}
c++
void main() {
auto routine_getTicker = exchange.Go("GetTicker");
auto routine_getDepth = exchange.Go("GetDepth");
auto routine_getTrades = exchange.Go("GetTrades");
auto ts1 = Unix() * 1000;
auto ret1 = EventLoop(0);
auto ts2 = Unix() * 1000;
auto ret2 = EventLoop(0);
auto ts3 = Unix() * 1000;
auto ret3 = EventLoop(0);
Log("First concurrent task completed:", _D(ts1), ret1);
Log("Second concurrent task completed:", _D(ts2), ret2);
Log("Third concurrent task completed:", _D(ts3), ret3);
Ticker ticker;
Depth depth;
Trades trades;
routine_getTicker.wait(ticker);
routine_getDepth.wait(depth);
routine_getTrades.wait(trades);
Log("GetTicker:", ticker);
Log("GetDepth:", depth);
Log("GetTrades:", trades);
}Returns
| Type | Description |
object | If the returned object is not empty, the
|
Arguments
| Name | Type | Required | Description |
timeout | number | No | The When |
See Also
Remarks
The event-listening mechanism is initialized only when the EventLoop() function is called for the first time in the code. If EventLoop() is first called after an event callback has already occurred, that earlier event will be missed. The queue structure encapsulated at the system's underlying level can cache at most 500 event callbacks; if the program does not call the EventLoop() function in time to retrieve them, later event callbacks exceeding the 500-cache limit will be lost.
Calling the EventLoop() function does not affect the underlying WebSocket cache queue of the system, nor does it affect the cache of concurrent functions such as exchange.Go(). The data in these caches must still be retrieved using their respective methods. For data that has already been retrieved before the EventLoop() function returns, no return event will be generated again in the EventLoop() function.
The main purpose of the EventLoop() function is to notify the strategy layer that the system's underlying level has received new network data, thereby driving the entire strategy in an event-driven manner. When the EventLoop() function returns an event, you only need to iterate through all data sources (such as WebSocket connections and objects created by exchange.Go()) and attempt to retrieve the data.
The EventLoop() function is only supported in live trading.
When called in the main function main(), it listens for events on the main thread. In strategies written in JavaScript, it can also be called in the execution function of a thread created by the threading.Thread() function to listen for events on the current thread.