digitalmars.D.learn - Waiting for a Spawn'ed process
- Sharad Gupta (7/7) Mar 28 2014 I am trying to make a Utility which spwans=>pipeShell off
- Vladimir Panteleev (3/10) Mar 28 2014 Calling wait on the Pid object returned by std.process functions
- Sharad Gupta (4/16) Mar 28 2014 But I don't have a Pid object but instead have Tid object from
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (27/42) Mar 28 2014 If you start the worker with spawnLinked then you will receive a
- Sharad Gupta (18/44) Mar 28 2014 But this is another spawned process. How can I tell this second
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/9) Mar 28 2014 std.concurrency is based on message passing. Normally, the second
I am trying to make a Utility which spwans=>pipeShell off multiple processes on user choice. On of the issues is that some process need to wait for other processes to finish. Now I could implement it using messages but that doesn't seem very nice solution. I could also wait for the pipeShell pid but was wondering what is the standard or recommended way to wait on a spawned process?
Mar 28 2014
On Friday, 28 March 2014 at 15:52:17 UTC, Sharad Gupta wrote:I am trying to make a Utility which spwans=>pipeShell off multiple processes on user choice. On of the issues is that some process need to wait for other processes to finish. Now I could implement it using messages but that doesn't seem very nice solution. I could also wait for the pipeShell pid but was wondering what is the standard or recommended way to wait on a spawned process?Calling wait on the Pid object returned by std.process functions is the standard way to wait for a process to finish in D.
Mar 28 2014
On Friday, 28 March 2014 at 16:37:00 UTC, Vladimir Panteleev wrote:On Friday, 28 March 2014 at 15:52:17 UTC, Sharad Gupta wrote:But I don't have a Pid object but instead have Tid object from the spawn function in std.concurrency lib.I am trying to make a Utility which spwans=>pipeShell off multiple processes on user choice. On of the issues is that some process need to wait for other processes to finish. Now I could implement it using messages but that doesn't seem very nice solution. I could also wait for the pipeShell pid but was wondering what is the standard or recommended way to wait on a spawned process?Calling wait on the Pid object returned by std.process functions is the standard way to wait for a process to finish in D.
Mar 28 2014
On 03/28/2014 10:19 AM, Sharad Gupta wrote:On Friday, 28 March 2014 at 16:37:00 UTC, Vladimir Panteleev wrote:If you start the worker with spawnLinked then you will receive a LinkTerminated message. import std.stdio; import std.concurrency; import core.thread; void main() { auto worker = spawnLinked(&workerFunc); // Wait for worker to terminate bool terminated = false; while (!terminated) { writeln("Waiting for the worker to terminate..."); terminated = receiveTimeout(1.seconds, (LinkTerminated e) { if (e.tid == worker) { writefln("Terminated"); } } ); } } void workerFunc() { Thread.sleep(3.seconds); } AliOn Friday, 28 March 2014 at 15:52:17 UTC, Sharad Gupta wrote:But I don't have a Pid object but instead have Tid object from the spawn function in std.concurrency lib.I am trying to make a Utility which spwans=>pipeShell off multiple processes on user choice. On of the issues is that some process need to wait for other processes to finish. Now I could implement it using messages but that doesn't seem very nice solution. I could also wait for the pipeShell pid but was wondering what is the standard or recommended way to wait on a spawned process?Calling wait on the Pid object returned by std.process functions is the standard way to wait for a process to finish in D.
Mar 28 2014
If you start the worker with spawnLinked then you will receive a LinkTerminated message. import std.stdio; import std.concurrency; import core.thread; void main() { auto worker = spawnLinked(&workerFunc); // Wait for worker to terminate bool terminated = false; while (!terminated) { writeln("Waiting for the worker to terminate..."); terminated = receiveTimeout(1.seconds, (LinkTerminated e) { if (e.tid == worker) { writefln("Terminated"); } } ); } } void workerFunc() { Thread.sleep(3.seconds); } AliBut this is another spawned process. How can I tell this second process to wait on the first one. eg: auto processA = spawnLinked(funcA); auto processB = spawnLinked(funcB); funcA() { doSomething(); } funcB() { doSomething(); // wait for A doSomethingElse(); } This is a simplified example, the real case two process would be spawned irrespective to each other. the second process could may also be launched by when the first is already done.
Mar 28 2014
On 03/28/2014 11:43 AM, Sharad Gupta wrote:But this is another spawned process. How can I tell this second process to wait on the first one.std.concurrency is based on message passing. Normally, the second process would send a message either to its owner or to another thread that was previously register()'ed. If message passing does not work for you then you may need to use lower-level modules like core.thread. Ali
Mar 28 2014