EventLoop
Listens for events. Returns when any WebSocket has readable data, or when a concurrent task such as exchange.Go() or HttpQuery_Go() completes.
EventLoop()
EventLoop(timeout)Examples
Fetch the ticker, order book depth and trade records concurrently, then use EventLoop to retrieve the completion events one by one.
javascript
function main() {
var routine_getTicker = exchange.Go("GetTicker")
var routine_getDepth = exchange.Go("GetDepth")
var routine_getTrades = exchange.Go("GetTrades")
// Concurrent tasks are registered when submitted: even if Sleep(2000) runs here first, the later EventLoop calls will not miss these tasks' completion events
// Log("GetDepth:", routine_getDepth.wait()) If wait is called here early to retrieve the result of the concurrent GetDepth call,
// the event for this GetDepth request result will no longer be returned by EventLoop
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 takes a typed token (e.g. Go::GetTicker) instead of a method-name string; 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, ());
// Concurrent tasks are registered when submitted: even if Sleep(2000) runs here first, the later EventLoop calls will not miss these tasks' completion events
// Log!("GetDepth:", routine_getDepth.wait(0)) If wait is called here early to retrieve the result of the concurrent GetDepth call,
// the event for this GetDepth request result will no longer be returned by EventLoop
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());
}Returns
| Type | Description |
object | An event object, where |
Arguments
| Name | Type | Required | Description |
timeout | number | No | Timeout in milliseconds. If set to |
See Also
Remarks
Concurrent tasks such as exchange.Go() and HttpQuery_Go() are registered when they are submitted. Their completion events are not missed, even if a task completes before EventLoop() is called for the first time. The underlying system uses a queue that caches up to 10240 events per thread. If the program does not call EventLoop() often enough to retrieve events, the oldest events are discarded once this limit is exceeded.
EventLoop() does not affect the system's underlying WebSocket cache queue or the caches of concurrent functions such as exchange.Go(). Data in those caches must still be retrieved with their own methods. If data is retrieved before EventLoop() returns, EventLoop() does not return an event for that data.
The main purpose of EventLoop() is to notify the strategy layer that the system has received new network data, so the whole strategy can be event-driven. When EventLoop() returns an event, go through all data sources and try to read from each one, such as WebSocket connections and objects created by exchange.Go().
EventLoop() 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 function run by a thread created with threading.Thread(). In that case, it listens for events on the current thread.
Exchange WebSocket pushes are subscribed to with ctx.subscribe and retrieved with ctx.poll. EventLoop() only subscribes automatically to market data and order updates for the current trading pair on broker-type exchanges that provide only a push interface. It does not subscribe to pushes for other exchanges. In JavaScript and Rust, EventLoop() and ctx.poll() in the same thread read events from the same event queue, so each thread should use only one of them. In Python, EventLoop() has its own queue and does not compete with ctx.poll().