or like this.
```
const { Worker, isMainThread } = require('worker_threads');
if (isMainThread) {
  let worker;
  while(global.stop !== 1) {
    if (!worker) {
      worker = new Worker(__filename);
    }
  }
} else {
  const inspector = require('inspector');
  const fs = require('fs');
  const session = new inspector.Session();
  session.connectToMainThread();
  session.post('Runtime.evaluate', {
    includeCommandLineAPI: true,
    expression: 'global.stop = 1',
  });
}
```
  
在2022年11月29日星期二 UTC+8 18:55:16<theratliter theratliter> 写道:

> Hi, all ! i remember that we can not run JS code in v8::RequestInterrupt, 
> But i find i can run JS code by posting a  `Runtime.evaluate` command to 
> V8  in v8::RequestInterrupt. My test is as follows (v8::RequestInterrupt is 
> used by session.connectToMainThread()  to post a message  to  v8).
> ```
> const { Worker, isMainThread } = require('worker_threads');
> if (isMainThread) {
>   new Worker(__filename);
>   while(1) {}
> } else {
>   const inspector = require('inspector');
>   const fs = require('fs');
>   const session = new inspector.Session();
>   session.connectToMainThread();
>   // post a message to main thread
>   session.post('Runtime.evaluate', {
>     includeCommandLineAPI: true, 
>     expression: 'process.pid',
>   }, (err, result) => {
>     fs.writeFileSync('1.txt', JSON.stringify(err || result));
>   });
> }
> ```
> So i wonder if we can run JS code by posting a  `Runtime.evaluate` command 
> to V8  in v8::RequestInterrupt ?
>

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-users/48927597-e8c7-4c54-8f2b-34ff7035de36n%40googlegroups.com.

Reply via email to