Make policy programs really run in parallel, adding multi-threaded support to JavaScript policies at the bottom of the system

Author: Inventors quantify - small dreams, Created: 2023-03-02 14:19:15, Updated: 2024-03-20 15:06:41

让策略程序真正并发执行,给JavaScript策略增加系统底层多线程支持

Make policy programs really run in parallel, adding multi-threaded support to JavaScript policies at the bottom of the system

When developing a policy on FMZ using JavaScript, the policy architecture is consulted; if there are scenarios of concurrent design, it is passed byexchange.GoFunctions to perform concurrent calls to some interfaces, thus fulfilling the needs of some concurrent scenarios. However, it is not possible to actually create a single thread to perform a series of operations if you want to do it separately, for example, as in the Python language.threadingI'm going to do some parallel designs.

Based on this need, the FMZ platform upgraded the system underpinnings. It also added true multi-threaded support for the JavaScript language. Detailed features include:

  • Create threads to execute custom functions simultaneously.
  • This is the first time I've seen this.
  • Variables stored between shared threads.
  • Waiting for the thread to finish the recovery resource and return the execution results.
  • Forced termination of threads and recycling of resources.
  • Retrieves the current thread ID in a concurrent thread execution function.

The following article will help you understand each function individually.

Create threads to execute custom functions simultaneously


function func1(sleepMilliseconds) { var sum = 0 for (var i = 0 ; i < 10 ; i++) { sum += i Sleep(sleepMilliseconds) Log(“sum:”, sum) }

return sum

}

function main (() { // Create a thread simultaneously using the __Thread function, with the parameter 200 being the parameter of the function function1 // If the function func1 has multiple parameters, here is the specific parameter that is passed var thread1Id = __Thread ((func1, 200)

// 这里需要等待线程Id为thread1Id的线程执行结果,否则main函数执行完就直接释放所有线程
var ret = __threadJoin(thread1Id)
Log("ret:", ret)

}


实际应用场景中,我们可以这样并发进行http请求:

function main() { let threads = [ “https://www.baidu.com”, “https://www.163.com” ].map(function(url) { return Thread(function(url) { Log(“GET”, url) return HttpQuery(url) }, url) }) threads.forEach(function(tid) { Log(threadJoin(tid)) }) }


### 等待线程执行结束回收资源并返回执行结果

以上例子中我们在main函数中最后使用了```__threadJoin```函数来等待并发的线程执行完毕,变量```ret```接收```__threadJoin```函数的返回值,我们打印了这个返回值,可以观察这个并发的线程执行的具体结果。

// id: thread ID, terminated: whether forced to stop, elapsed: elapsed ((nanoseconds), ret: return value of thread execution function Ret: {Full name of the person who is responsible for the death of a child}


### 强制结束线程,并回收资源

function func1(sleepMilliseconds) { var sum = 0 for (var i = 0 ; i < 10 ; i++) { sum += i Sleep(sleepMilliseconds) Log(“sum:”, sum) }

return sum

}

function main() { var thread1Id = __Thread(func1, 200) Sleep(1000) retThreadTerminate = __threadTerminate(thread1Id) Log(retThreadTerminate) // true }


还是以刚才的例子,在创建线程后,可以在等待1秒之后就强制终止线程执行。

### 线程间通信

线程间通信主要使用```__threadPostMessage```函数和```__threadPeekMessage```函数。我们来看以下简单例子:

function1 (()) { var id = __threadId (()) while (true) { var postMsg = message bar from id:func1 of the thread function func + id + func __threadPostMessage ((0, postMsg) // sends a message to the main thread var peekMsg = __threadPeekMessage() // Receive messages from other threads Log ((peekMsg)) Sleep ((5000) I'm not sure. I'm not sure.

function main() { var threadId = __Thread(func1)

while (true) {
    var postMsg = "来自主线程的main函数的消息"
    __threadPostMessage(threadId, postMsg)
    var peekMsg = __threadPeekMessage()
    Log(peekMsg, "#FF0000")                     // #FF0000 , 设置日志为红色用于区分
    Sleep(5000)
}

}


```__threadPostMessage```函数用于向某个线程发送消息,第一个参数是具体发送到哪个线程的ID,第二个参数是发送的消息,可以是字符串、数值、数组、JSON对象等。可以在并发的线程函数中向主线程发送消息,主线程的ID定义为0。

```__threadPeekMessage```函数用于监听某个线程发送来的消息, 可以设置超时时间(毫秒数),也可以设置为0表示阻塞,一直监听到有消息才返回。

当然,除了并发的线程和主线程通信。并发的线程之间也可以直接相互通信。

### 在并发的线程执行函数中获取当前线程ID

在上面的例子中,使用了```var id = __threadId()```,```__threadId()```函数可以获取当前线程的ID。

### 共享线程间储存的变量

除了线程间的通信,还可以使用共享变量进行交互。

function testFunc (()) { __threadSetData ((0, testFunc, 100) // stores the current thread environment with key value for testFunc: 100 Log ((testFunc is executing the completion function)) I'm not sure.

function main (() { // threadId is 1, the thread created with threadId is executed first, as long as the thread resource is not recovered and the variable stored locally in the thread is valid var testThread = __Thread ((testFunc))

Sleep(1000)

// 输出 in main, get testFunc: 100
Log("in main, get testFunc:", __threadGetData(testThread, "testFunc"))   // 取出键名为testFunc的值

} “`

This is a simple demonstration of all the functions.


Related

More

The bone knifeError ReferenceError: '__Thread' is not defined at main (__FILE__:5)

The bone knifeCollecting and Learning

Inventors quantifiedUpgrading the Trustee