Type/to search
Built-in Functions
Global
Version
Sleep
IsVirtual
Mail
Mail_Go
SetErrorFilter
GetPid
GetLastError
GetCommand
GetMeta
Dial
HttpQuery
HttpQuery_Go
Encode
UnixNano
Unix
GetOS
MD5
DBExec
UUID
EventLoop
__Serve
_G
_D
_N
_C
_Cross
JSON.parse
JSON.stringify
SetChannelData
GetChannelData
Log
Market
Trade
Account
Futures
NetSettings
Threads
threading
Thread
getThread
mainThread
currentThread
Lock
Condition
Event
Dict
pending
Thread
ThreadLock
ThreadEvent
ThreadCondition
ThreadDict
Web3
TA
Talib
talib.CDL2CROWS
talib.CDL3BLACKCROWS
talib.CDL3INSIDE
talib.CDL3LINESTRIKE
talib.CDL3OUTSIDE
talib.CDL3STARSINSOUTH
talib.CDL3WHITESOLDIERS
talib.CDLABANDONEDBABY
talib.CDLADVANCEBLOCK
talib.CDLBELTHOLD
talib.CDLBREAKAWAY
talib.CDLCLOSINGMARUBOZU
talib.CDLCONCEALBABYSWALL
talib.CDLCOUNTERATTACK
talib.CDLDARKCLOUDCOVER
talib.CDLDOJI
talib.CDLDOJISTAR
talib.CDLDRAGONFLYDOJI
talib.CDLENGULFING
talib.CDLEVENINGDOJISTAR
talib.CDLEVENINGSTAR
talib.CDLGAPSIDESIDEWHITE
talib.CDLGRAVESTONEDOJI
talib.CDLHAMMER
talib.CDLHANGINGMAN
talib.CDLHARAMI
talib.CDLHARAMICROSS
talib.CDLHIGHWAVE
talib.CDLHIKKAKE
talib.CDLHIKKAKEMOD
talib.CDLHOMINGPIGEON
talib.CDLIDENTICAL3CROWS
talib.CDLINNECK
talib.CDLINVERTEDHAMMER
talib.CDLKICKING
talib.CDLKICKINGBYLENGTH
talib.CDLLADDERBOTTOM
talib.CDLLONGLEGGEDDOJI
talib.CDLLONGLINE
talib.CDLMARUBOZU
talib.CDLMATCHINGLOW
talib.CDLMATHOLD
talib.CDLMORNINGDOJISTAR
talib.CDLMORNINGSTAR
talib.CDLONNECK
talib.CDLPIERCING
talib.CDLRICKSHAWMAN
talib.CDLRISEFALL3METHODS
talib.CDLSEPARATINGLINES
talib.CDLSHOOTINGSTAR
talib.CDLSHORTLINE
talib.CDLSPINNINGTOP
talib.CDLSTALLEDPATTERN
talib.CDLSTICKSANDWICH
talib.CDLTAKURI
talib.CDLTASUKIGAP
talib.CDLTHRUSTING
talib.CDLTRISTAR
talib.CDLUNIQUE3RIVER
talib.CDLUPSIDEGAP2CROWS
talib.CDLXSIDEGAP3METHODS
talib.AD
talib.ADOSC
talib.OBV
talib.ACOS
talib.ASIN
talib.ATAN
talib.CEIL
talib.COS
talib.COSH
talib.EXP
talib.FLOOR
talib.LN
talib.LOG10
talib.SIN
talib.SINH
talib.SQRT
talib.TAN
talib.TANH
talib.MAX
talib.MAXINDEX
talib.MIN
talib.MININDEX
talib.MINMAX
talib.MINMAXINDEX
talib.SUM
talib.HT_DCPERIOD
talib.HT_DCPHASE
talib.HT_PHASOR
talib.HT_SINE
talib.HT_TRENDMODE
talib.ATR
talib.NATR
talib.TRANGE
talib.BBANDS
talib.DEMA
talib.EMA
talib.HT_TRENDLINE
talib.KAMA
talib.MA
talib.MAMA
talib.MIDPOINT
talib.MIDPRICE
talib.SAR
talib.SAREXT
talib.SMA
talib.T3
talib.TEMA
talib.TRIMA
talib.WMA
talib.LINEARREG
talib.LINEARREG_ANGLE
talib.LINEARREG_INTERCEPT
talib.LINEARREG_SLOPE
talib.STDDEV
talib.TSF
talib.VAR
talib.ADX
talib.ADXR
talib.APO
talib.AROON
talib.AROONOSC
talib.BOP
talib.CCI
talib.CMO
talib.DX
talib.MACD
talib.MACDEXT
talib.MACDFIX
talib.MFI
talib.MINUS_DI
talib.MINUS_DM
talib.MOM
talib.PLUS_DI
talib.PLUS_DM
talib.PPO
talib.ROC
talib.ROCP
talib.ROCR
talib.ROCR100
talib.RSI
talib.STOCH
talib.STOCHF
talib.STOCHRSI
talib.TRIX
talib.ULTOSC
talib.WILLR
talib.AVGPRICE
talib.MEDPRICE
talib.TYPPRICE
talib.WCLPRICE
OS
Structures
Built-in Variables

Thread objects can be created or returned through threading.Thread(), threading.getThread(), threading.mainThread(), threading.currentThread().

The peekMessage() function is used to receive messages from a thread.

peekMessage()
peekMessage(timeout)

Examples

Concurrent thread sends messages to the main thread.

javascript
function main() { var t1 = threading.Thread(function() { for (var i = 0; i < 10; i++) { Log("thread1 postMessage():", i) threading.mainThread().postMessage(i) Sleep(500) } }) while (true) { var msg = threading.currentThread().peekMessage() Log("main peekMessage():", msg) if (msg == 9) { break } Sleep(1000) } t1.join() }

Returns

TypeDescription

string / number / bool / object / array / any (any type supported by the platform)

The peekMessage() function returns messages received by the thread associated with the current thread object.

Arguments

NameTypeRequiredDescription

timeout

number

No

The timeout parameter sets the timeout duration, blocking and waiting for data according to the milliseconds specified by this parameter; returns null if no data is received and timeout occurs. If timeout is set to 0 or the timeout parameter is not passed, it will block indefinitely until data is received from the channel. If timeout is set to -1, it will not block and returns immediately, returning null when there is no data.

See Also

Remarks

When writing programs, be careful to avoid thread deadlock issues.

The postMessage() function is used to send messages to a thread.

postMessage(msg)

Examples

  • Send messages in concurrent threads and use eventLoop() to receive message notifications.

    javascript
    function main() { var t1 = threading.Thread(function() { for (var i = 0; i < 10; i++) { Log("thread1 postMessage():", i) threading.mainThread().postMessage(i) Sleep(500) } }) for (var i = 0; i < 10; i++) { var event = threading.mainThread().eventLoop() Log("main event:", event) Sleep(500) } t1.join() }
  • Supports sending functions.

    javascript
    function main() { threading.mainThread().postMessage(function(msg) { Log("func from mainThread, msg:", msg) }) threading.Thread(function() { var func = threading.mainThread().peekMessage() func("in " + threading.currentThread().name()) }).join() }

Arguments

NameTypeRequiredDescription

msg

string / number / bool / object / array / function / any (any type supported by the platform)

Yes

The parameter msg is the message to be sent.

See Also

Remarks

When the postMessage() function is called in a thread's execution function to send signals or data, it generates a message event. You can use the eventLoop() function to receive message notifications.

The join() function is used to wait for a thread to exit and reclaim system resources.

join()
join(timeout)

Examples

Test join() function timeout and output the return value.

javascript
function main() { var t1 = threading.Thread(function() { Log("Hello thread1") Sleep(5000) }) var ret = t1.join(1000) Log("ret:", ret) // ret: undefined ret = t1.join() Log("ret:", ret) // ret: {"id":1,"terminated":false,"elapsed":5003252000} }

Returns

TypeDescription

ThreadRet object

The ThreadRet object contains data related to the execution result, including the following properties:

  • id: Thread ID.
  • terminated: Whether the thread was forcibly terminated.
  • elapsed: The running time of the thread (nanoseconds).
  • ret: The return value of the thread function.

Arguments

NameTypeRequiredDescription

timeout

number

No

The timeout parameter is used to set the timeout for waiting for the thread to end, in milliseconds. When the timeout parameter is set to 0 or the timeout parameter is not set, the join() function will block until the thread finishes execution. When the timeout parameter is set to -1, the join() function will return immediately.

See Also

Remarks

When the join() function times out, it returns undefined.

The terminate() function is used to forcibly terminate a thread and release the hardware resources occupied when the thread was created.

terminate()

Examples

Forcibly terminate the execution of a thread. After forcibly terminating the thread, the content output by that thread will no longer be displayed in the logs.

javascript
function main() { var t1 = threading.Thread(function() { for (var i = 0; i < 10; i++) { Log("thread1 i:", i) Sleep(1000) } }) Sleep(3000) t1.terminate() Log("after t1.terminate()") while (true) { LogStatus(_D()) Sleep(1000) } }

See Also

Remarks

For threads forcibly terminated using the terminate() function, the join() function can no longer be used to wait for their completion.

The getData() function is used to access variables recorded in the thread environment. The data is valid when the thread has not executed the join() function (waiting for successful exit) and has not executed the terminate() function (forcibly terminating the thread).

getData()
getData(key)

Examples

Record a value with the key name count in the concurrent thread environment, then read the key value of count in the main thread.

javascript
function main() { var t1 = threading.Thread(function() { for (var i = 0; i < 5; i++) { threading.currentThread().setData("count", i) Log(`setData("count"):`, i) Sleep(1000) } }) for (var i = 0; i < 5; i++) { var count = threading.getThread(t1.id()).getData("count") Log(`getData("count"):`, count) Sleep(1000) } t1.join() }

Returns

TypeDescription

string / number / bool / object / array / any (any type supported by the platform)

The getData() function returns the key value corresponding to the key parameter in the key-value pairs stored in the current thread environment.

Arguments

NameTypeRequiredDescription

key

string

Yes

The key parameter is the key name of the stored key-value pair.

See Also

The setData() function is used to store variables in the thread environment.

setData(key, value)

Examples

  • Set a key-value pair in a concurrent thread and read the key-value pair in the main thread.

    javascript
    function main() { var t1 = threading.Thread(function() { threading.currentThread().setData("data", 100) }) Sleep(1000) Log(`t1.getData("data"):`, t1.getData("data")) t1.join() }
  • Supports passing functions as key values.

    javascript
    function main() { threading.mainThread().setData("func2", function(p) { Log("func2 p:", p) }) var t1 = threading.Thread(function() { threading.currentThread().setData("func1", function(p) { Log("func1 p:", p) }) var func2 = threading.mainThread().getData("func2") func2("test2") }) Sleep(1000) var func1 = t1.getData("func1") func1("test1") t1.join() }

Arguments

NameTypeRequiredDescription

key

string

Yes

The key parameter is used to specify the key name of the key-value pair to be stored.

value

string / number / bool / object / array / function / any (any type supported by the platform)

Yes

The value parameter is used to specify the key value of the key-value pair to be stored.

See Also

Remarks

Data remains valid as long as the thread has not executed the join() function (waiting for successful exit) and has not executed the terminate() function (forcibly terminating the thread). The value parameter must be a serializable variable.

The id() function is used to return the threadId of the current multi-threaded object instance.

id()

Examples

Create a concurrently running thread and output the threadId of that concurrent thread in the main thread.

javascript
function main() { var t1 = threading.Thread(function() { threading.currentThread().setData("data", 100) }) Log(`t1.id():`, t1.id()) t1.join() }

Returns

TypeDescription

number

The id() function returns the threadId.

See Also

The name() function is used to return the name of the current multi-threaded object instance.

name()

Examples

Create a concurrently running thread and output the name of that concurrent thread in the main thread.

javascript
function main() { var t1 = threading.Thread(function() { threading.currentThread().setData("data", 100) }) Log(`t1.name():`, t1.name()) // t1.name(): Thread-1 t1.join() }

Returns

TypeDescription

string

The name() function returns the name of the concurrent thread.

See Also

The eventLoop() function is used to listen for events received by the thread.

eventLoop()
eventLoop(timeout)

Examples

Execute 3 threads concurrently and output the received event information. When timeout occurs or immediate return, the output value is null.

javascript
function main() { var t1 = threading.Thread(function() { while (true) { var eventMsg = threading.currentThread().eventLoop() // 阻塞等待 // 2024-11-14 10:14:18 thread1 eventMsg: {"Seq":1,"Event":"thread","ThreadId":0,"Index":1,"Queue":0,"Nano":1731550458699947000} Log(_D(), "thread1 eventMsg:", eventMsg) } }) var t2 = threading.Thread(function() { while (true) { var eventMsg = threading.currentThread().eventLoop(-1) // 立即返回 Log(_D(), "thread2 eventMsg:", eventMsg) Sleep(5000) } }) var t3 = threading.Thread(function() { while (true) { var eventMsg = threading.currentThread().eventLoop(3000) // 设置3秒超时 Log(_D(), "thread3 eventMsg:", eventMsg) } }) t1.postMessage("Hello ", t1.name()) t2.postMessage("Hello ", t2.name()) t3.postMessage("Hello ", t3.name()) t1.join() t2.join() t3.join() }

Returns

TypeDescription

object / null

The eventLoop() function returns the event information received by the current thread. See Event Information Structure for details.

Arguments

NameTypeRequiredDescription

timeout

number

No

The parameter timeout is the timeout setting in milliseconds. When the parameter timeout is set to 0, the function will block and wait until an event occurs before returning; when set to a value greater than 0, the function will return after the specified timeout period; when set to a value less than 0, the function will immediately return the most recent event.

See Also

Remarks

The processing mechanism of the eventLoop() function is consistent with the global function EventLoop().