lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> while true do os.execute('echo hello world') end
> 
> cannot be stopped with ^C.

Thanks everybody you explained the question.  Now a somewhat related question:

Is there any way to pause a lua program for less than one second?

Thanks to your help I can write a loop like this:

while true do 
	condition_met = some_function_that_should_no_be_done_too_often()
	if condition_met or not os.execute('sleep 1') then break end 
end

Which will end the loop if either the condition is met or the user gets bored and hits ^C

Obviously if the other function were lua I could use coroutines but if the other function is a separate program I am calling with os.execute and it uses significant system resources to do its checking I need some way to wait a reasonable time before calling it again.

Is there any way to make this time less than one second? 	
 



On May 14, 2013, at 7:15 AM, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
>> 
> 
> The ^C is caught by the forked process and is never seen by Lua. Try
> 	while true do os.execute('echo hello ; sleep 5; echo bye') end
> 
> When you press ^C, you never see "bye".
> 
> Now try hitting ^C on this code:
> 	while os.execute("echo hello") do end
> 
> If you print the return value, you'll see
> 	true	exit	0
> 	hello
> 	true	exit	0
> 	hello
> 	true	exit	0
> 	^Cnil	signal	2
> 
> Bottom line: test the return value of os.execute if you want to handle ^C
> in forked processes.
>