worker_threads doesn't support it?
Hello @2505817596,
If you're referring to worker_threads, that's a Node.js API. It isn't part of the standard JavaScript environment that ClearScript provides. You can of course expose a threading API based on .NET threads to your script environment.
Good luck!
如果你指的是worker_threads,那是一个节点.js API。它不是清除脚本提供的标准 JavaScript 环境的一部分。当然,您可以将基于.NET线程的线程API暴露到脚本环境中。
祝你好运!
Thank you for responding so quickly.
But in fact, APIs like Task.Run() can only run on clearscript in a single thread.
There is no solution to achieve multithreading that can run in clearscript
Hi @2505817596
But in fact, APIs like Task.Run() can only run on clearscript in a single thread.
Please clarify. Task.Run is a .NET API; it neither requires ClearScript nor is affected by ClearScript in any way.
Here's an example that invokes Task.Run from script code to execute script code on a worker thread.
C#
using (var engine = new V8ScriptEngine()) {
engine.AddHostType(typeof(Console));
engine.AddHostType(typeof(Task));
engine.AddHostType(typeof(Action));
engine.Script.Done = new ManualResetEvent(false);
engine.Execute(@"
Task.Run(new Action(() => {
Console.WriteLine('Hello from the thread pool!');
Done.Set();
}));
");
engine.Script.Done.WaitOne();
}
There is no solution to achieve multithreading that can run in clearscript
The .NET platform provides threading APIs, and you can use ClearScript to expose a threading API to the script environment.
An individual instance of a script engine cannot execute script code simultaneously on multiple threads, but you can use multiple instances for that. Also, some script engines, such as V8, can execute script code _sequentially_ on multiple threads. In fact, V8's threading requirements and behavior are exactly the same in ClearScript as they are in Node.js.
Cheers!
Hi @2505817596
But in fact, APIs like Task.Run() can only run on clearscript in a single thread.
Please clarify.
Task.Runis a .NET API; it neither requires ClearScript nor is affected by ClearScript in any way.Here's an example that invokes
Task.Runfrom script code to execute script code on a worker thread.using (var engine = new V8ScriptEngine()) { engine.AddHostType(typeof(Console)); engine.AddHostType(typeof(Task)); engine.AddHostType(typeof(Action)); engine.Script.Done = new ManualResetEvent(false); engine.Execute(@" Task.Run(new Action(() => { Console.WriteLine('Hello from the thread pool!'); Done.Set(); })); "); engine.Script.Done.WaitOne(); }There is no solution to achieve multithreading that can run in clearscript
The .NET platform provides threading APIs, and you can use ClearScript to expose a threading API to the script environment.
An individual instance of a script engine cannot execute script code simultaneously on multiple threads, but you can use multiple instances for that. Also, some script engines, such as V8, can execute script code _sequentially_ on multiple threads. In fact, V8's threading requirements and behavior are exactly the same in ClearScript as they are in Node.js.
Cheers!
Thank you for answering my doubts, and finally I want to know, a program runs
Can 8 examples be enough, or more examples?
finally I want to know, a program runs
Can 8 examples be enough, or more examples?
Sorry, we're not sure what you mean. Are you looking for more examples? Please let us know specifically what you're looking for.
finally I want to know, a program runs
Can 8 examples be enough, or more examples?Sorry, we're not sure what you mean. Are you looking for more examples? Please let us know specifically what you're looking for.
thank you for your reply,
I just want to know if multiple V8ScriptEngine instances will put pressure on the program, such as memory overhead. My program has 8 V8ScriptEngine instances.
Hi @2505817596,
I just want to know if multiple V8ScriptEngine instances will put pressure on the program, such as memory overhead. My program has 8 V8ScriptEngine instances.
Oh yes, that should be fine. Although V8 heavily favors performance over memory efficiency, 8 instances shouldn't be enough to cause problems, even in a 32-bit process. We have tests that use many more than that.
Our usual recommendation for multithreaded script applications is to use approximately as many V8ScriptEngine instances as you have CPU cores.
Good luck!
我只想知道多个V8Script工程实例是否会给程序带来压力,例如内存开销。我的程序有8个V8脚本工程实例。
哦,是的,那应该没问题。虽然 V8 非常倾向于性能而不是内存效率,但 8 个实例不应足以导致问题,即使在 32 位过程中也是如此。我们有比这多得多的测试。
我们通常对多读脚本应用程序的建议是使用与 CPU 内核大致相同多的实例。
V8ScriptEngine祝你好运!
Thank you for solving my doubts
Please reopen this issue if you have additional questions. Thank you!
Please reopen this issue if you have additional questions. Thank you!
Please reopen this issue if you have additional questions. Thank you!
Hi, I encountered a new problem again,
The following code:
v8ScriptEngine1.Execute(new DocumentInfo {Category = ModuleCategory.CommonJS }, File.ReadAllText("main.js"));
How to get the script resources after execution.
I tried to export through the following code
var app = function () {
console.log("Im an application!");
}
exports.app = app
But v8ScriptEngine1.Script does not see the app
Hello @2505817596,
In a module, the var statement does _not_ create a global property.
Here's an example that might help. Suppose that your module file, main.js, looks like this:
exports.app = function () {
Console.WriteLine("I'm an application!");
};
Here's how you might use the module from script code:
``` C#
engine.AddHostType(typeof(Console));
engine.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableFileLoading;
engine.Evaluate(new DocumentInfo { Category = ModuleCategory.CommonJS }, @"
require('main.js').app(); // output: I'm an application!
");
Now, if you want to access the module from .NET, you can do it like this:
``` C#
engine.Execute(new DocumentInfo { Category = ModuleCategory.CommonJS }, @"
main = require('main.js');
");
engine.Script.main.app(); // output: I'm an application!
You could also use globalThis to manipulate the global object from module code, although that's often considered undesirable, as it introduces a side effect.
Good luck!
Hello @2505817596,
In a module, the
varstatement does _not_ create a global property.Here's an example that might help. Suppose that your module file,
main.js, looks like this:exports.app = function () { Console.WriteLine("I'm an application!"); };Here's how you might use the module from script code:
engine.AddHostType(typeof(Console)); engine.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableFileLoading; engine.Evaluate(new DocumentInfo { Category = ModuleCategory.CommonJS }, @" require('main.js').app(); // output: I'm an application! ");Now, if you want to access the module from .NET, you can do it like this:
engine.Execute(new DocumentInfo { Category = ModuleCategory.CommonJS }, @" main = require('main.js'); "); engine.Script.main.app(); // output: I'm an application!You could also use
globalThisto manipulate the global object from module code, although that's often considered undesirable, as it introduces a side effect.Good luck!
Thank you for solving my problem
Hello @2505817596,
In a module, the
varstatement does _not_ create a global property.Here's an example that might help. Suppose that your module file,
main.js, looks like this:exports.app = function () { Console.WriteLine("I'm an application!"); };Here's how you might use the module from script code:
engine.AddHostType(typeof(Console)); engine.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableFileLoading; engine.Evaluate(new DocumentInfo { Category = ModuleCategory.CommonJS }, @" require('main.js').app(); // output: I'm an application! ");Now, if you want to access the module from .NET, you can do it like this:
engine.Execute(new DocumentInfo { Category = ModuleCategory.CommonJS }, @" main = require('main.js'); "); engine.Script.main.app(); // output: I'm an application!You could also use
globalThisto manipulate the global object from module code, although that's often considered undesirable, as it introduces a side effect.Good luck!
Hi, may I ask
Pass require('main.js')
How to delete module cache
require.cache//undefined
Hi @2505817596,
ClearScript doesn't support require.cache; that's a Node.js extension of the CommonJS module specification. V8ScriptEngine internally caches compiled modules, but that cache is inaccessible to script code and the host.
However, ClearScript's document loader caches _documents_ (raw file contents) to avoid redundant disk and network access. You can clear that cache as follows:
C#
engine.DocumentSettings.Loader.DiscardCachedDocuments();
Note that the document loader's cache is used across engine instances.
Cheers!
Note that the document loader's cache is used across engine instances.
Thanks, this is exactly what I need.
Hi, are you free, at the end you mentioned
document loader's cache is used across engine instances.
Can you write an example to use in multiple engine instances
Can you write an example to use in multiple engine instances
By default, all script engine instances share the same document loader, so clearing the document cache via one instance affects the others. If you need separate document caches for different instances, you'll have to create custom document loaders.
Most helpful comment
Thank you for solving my doubts