Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #36933
| From | Richard Maher <maher_rjSPAMLESS@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Simulating synchronous I/O behavior with worker threads |
| Date | 2019-06-18 18:00 +0800 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <qeacob$1ni8$2@gioia.aioe.org> (permalink) |
| References | <8df88a58-4f42-496f-8ae9-bd8b7f367a6b@googlegroups.com> |
On 28/05/2019 1:22 pm, mertesthomas@gmail.com wrote:
> Hello,
>
> I want to simulate synchronous I/O behavior with JavaScript.
> I know that JavaScript works asynchronous and that callbacks,
> Promises and other technologies are used, to avoid that a
> program that hangs while it waits for something.
>
> If you have ideas, how synchronous behavior can be emulated,
> please tell me.
>
> I looked at the worker_threads provided by node.js. I was hoping
> to be able to simulate synchronous behavior. My idea was:
> The main thread is not allow to hang (wait for some I/O), but
> the worker thread may be allowed to do so.
>
> When a worker thread is started it can get some workerData from
> the main thread. A worker_thread can also send something back to
> the main thread via a parentPort. To receive the message from the
> worker_thread the main thread uses something like
>
> worker.on('message', (msg) => { console.log(msg); });
>
> and then terminates. The message from the worker_thread is then
> handled in this callback. I have found also examples, where the
> worker_thread registers some callback and terminates itself to
> receive a message from the main thread. It seems that this works
> like anything else in JavaScript: asynchronous
>
> What I am missing is: The worker thread should be able to wait
> for a message (without callback and terminating the worker_thread)
> and to continue after the message has been received. It is
> important that no termination of the thread is necessary and
> that the program can continue after the "wait" statement.
>
> A not so elegant solution would be possible, when there is the
> possibility to poll, if a message has been sent. In this case
> the worker_thread could poll for a message (possibly with some
> sleep in between).
>
> I can understand that the main thread works asynchronous and
> never waits for something. But for worker_threads this could
> be different.
>
> I was thinking to start a worker_thread with the main program
> and the main thread would only handle events and sending them
> to the worker_thread. To do this the worker_thread must be
> able to work synchronous (wait for messages from the main thread).
>
> Is this possible?
>
> Regards,
> Thomas Mertes
>
Have you looked up "await" and "async"? It is the illusion of
synchronicity but what do you care?
Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread
Simulating synchronous I/O behavior with worker threads mertesthomas@gmail.com - 2019-05-27 22:22 -0700 Re: Simulating synchronous I/O behavior with worker threads Richard Maher <maher_rjSPAMLESS@hotmail.com> - 2019-06-18 18:00 +0800
csiph-web