Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #36652 > unrolled thread

Simulating synchronous I/O behavior with worker threads

Started bymertesthomas@gmail.com
First post2019-05-27 22:22 -0700
Last post2019-06-18 18:00 +0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#36652 — Simulating synchronous I/O behavior with worker threads

Frommertesthomas@gmail.com
Date2019-05-27 22:22 -0700
SubjectSimulating synchronous I/O behavior with worker threads
Message-ID<8df88a58-4f42-496f-8ae9-bd8b7f367a6b@googlegroups.com>
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

-- 
Seed7 Homepage:  http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.

[toc] | [next] | [standalone]


#36933

FromRichard Maher <maher_rjSPAMLESS@hotmail.com>
Date2019-06-18 18:00 +0800
Message-ID<qeacob$1ni8$2@gioia.aioe.org>
In reply to#36652
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?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web