Jump to content




[Tutorial] Parallel API


  • You cannot reply to this topic
35 replies to this topic

#21 Darky_Alan

  • Members
  • 89 posts
  • LocationPuerto Rico

Posted 24 July 2012 - 08:37 AM

View PostNoodle, on 24 July 2012 - 08:34 AM, said:

^ What I meant is that its so short that it will keep receiving with no lag 0.0
Read wiki on sockets... Kinda hard to explain; Wiki
Its similar to that, that is more networking.

I haven't even bothered working with rednet yet, I'll poke around with it later. I'll bookmark the link for now, thanks.

#22 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 24 July 2012 - 08:41 AM

Well its not only used for that, Its more just and explanation of what a socket is
Like pistons
Say you have 2 pistons
Both are 2 different functions.
You can only power one at a time from 1 side.
0
0 -------+
--------------------- OR
0 -------+
0

Using this api it opens the other side so they can both be powered at the same time.
+ -------- 0
0 -------- +

#23 jamiemac262

  • New Members
  • 2 posts

Posted 16 August 2013 - 02:59 PM

okay so what this sounds like to me is parallel doesnt mean paralell, it mean one then the other

so how do i make this work

it's supposed to run an alarm using immibis peripheral speaker until user enters password

function alarm()
p = peripheral.wrap("speaker_1")
p.start(1, 500)
sleep(1)
p.stop(1)
p.start(1, 400)
sleep(1)
p.stop(1)
alarm()
end

function relog()
term.clear()
term.setCursorPos(1,1)
print "Enter Your Password"
pass = io.read()
if pass == "password" then
return

else
term.clear()
print "ERROR:X101:: Incorrect Password"
print "Press [ENTER] to continue"
io.read()
relog()
end
end

parallel.waitForAny(relog(), alarm())
p.stop(1)
shell.exit()

#24 svdragster

  • Members
  • 222 posts
  • LocationGermany

Posted 16 August 2013 - 03:28 PM

View Postjamiemac262, on 16 August 2013 - 02:59 PM, said:

okay so what this sounds like to me is parallel doesnt mean paralell, it mean one then the other

so how do i make this work

it's supposed to run an alarm using immibis peripheral speaker until user enters password

I think you have to remove the brackets
parallel.waitForAny(relog(), alarm())
parallel.waitForAny(relog, alarm)

#25 DarkEspeon

  • Members
  • 57 posts

Posted 19 August 2013 - 08:14 PM

Actually, what parrallel does, is what i believe to be like what a computer does, it alternates between the two extremely quickly, like it gives computations to one for mere milliseconds, then switches and gives the next milliseconds, ect. , with wait for any until one returns, and with waitforall it waits for all to return

#26 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 August 2013 - 09:25 PM

View PostDarkEspeon, on 19 August 2013 - 08:14 PM, said:

Actually, what parrallel does, is what i believe to be like what a computer does, it alternates between the two extremely quickly, like it gives computations to one for mere milliseconds, then switches and gives the next milliseconds, ect. , with wait for any until one returns, and with waitforall it waits for all to return
Kinda. What coroutines do in Lua is slightly different than what the computer does (in a multiprocessor machine). With coroutines, no matter the system only one will run at a time and the next will not run until the current one has yielded. This is why in CCLua we have the "too long without yielding error". This differs from the (multiprocessor) computers running threads as they are actually able to run multiple threads concurrently.
The main point to take from what I said here is that the coroutine must yield, control is not revoked every millisecond, the routine must yield its control.

#27 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 15 September 2013 - 06:20 PM

The last loop is unnecessary, since send and recv never return.

#28 JuanDaniel2510

  • New Members
  • 2 posts
  • LocationSpain

Posted 30 October 2013 - 07:03 AM

How can I do to send arguments without using global variables in parallel functions?

I know I can't do this parallel.waitForAny( func1(arg1,arg2,...), func2(arg1,arg2,...),...)

#29 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 30 October 2013 - 07:07 AM

View PostJuanDaniel2510, on 30 October 2013 - 07:03 AM, said:

How can I do to send arguments without using global variables in parallel functions?

I know I can't do this parallel.waitForAny( funt1(arg1,arg2,...), funt2(arg1,arg2,...),...)

you can cheat a little and do this
parallel.waitForAny(
 function()
  funt1(arg1,arg2,...)
 end
,
 function()
  funt2(arg1,arg2,...)
 end
,...
)


#30 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 11 November 2013 - 08:58 AM

There's another way; you can have your function return another function to actually be used as a parallel. Note that it's usually only best used when you want to use a single function many times, but with different arguments.

function foo(msg, num)
	return function()
		for i=1, num do
			print(msg)
			sleep(math.random() * 2 + 1)
		end
	end
end

parallel.waitForAll(
	foo("hello", 10), --# returns a function that will print "hello" 10 times
	foo("world", 20)  --# returns a function that will print "world" 20 times
)

Edited by Kingdaro, 11 November 2013 - 08:58 AM.


#31 Cozzimoto

  • Members
  • 221 posts
  • LocationDallas, Tx

Posted 15 November 2013 - 05:43 PM

@Noodle
i noticed in your OP you have the while loops within the functions your going to run in parallel. My question is wouldnt you want the while loop to run the parallel like so

local func1 = function()
  -- CODE HERE
end
local func2 = function()
  -- MORE CODE
end

while true do
  parallel.waitForAny( func1, func2 )

end

If there is a difference what would your advantages/disadvantages be?

#32 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 15 November 2013 - 06:57 PM

waitForAny ends when any one of functions ends - all other get terminated. so if func1 ends then func2 if forcefully ended. This can cause weird/unpredictable. situations. bye using while true end loops inside functions you ensure they never end - so there will never be said situation.
At same time this can be what you need in your program soo....

Using waitForAll has this difference from Any that it waits for all to end - so if func1 ended it will wait for func2. This is again different situation - and that means you can never bee sure that both are run "at same" time.

So basically using any and while true end loops is usually exactly what you need from 3 hypothetical situations i presented here.

#33 Cozzimoto

  • Members
  • 221 posts
  • LocationDallas, Tx

Posted 15 November 2013 - 07:24 PM

Thanks for the clarification Wojbie, i appreciate it +1

#34 distantcam

  • Members
  • 139 posts
  • LocationChunk 0

Posted 15 November 2013 - 09:32 PM

View Postwojbie, on 15 November 2013 - 06:57 PM, said:

waitForAny ends when any one of functions ends - all other get terminated.

I'm not so sure about that second bit. waitForAny does return after one of the functions ends, but I think the other functions are suspended and can be resumed, rather than terminated.

#35 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 15 November 2013 - 11:03 PM

View Postdistantcam, on 15 November 2013 - 09:32 PM, said:

I'm not so sure about that second bit. waitForAny does return after one of the functions ends, but I think the other functions are suspended and can be resumed, rather than terminated.
If you have references to the coroutines then yes you could resume it, but the way that most people use parallel.waitForAny means that once one function is completed the others will become suspended, and eventually killed and cleaned up by the Java Garbage Collector...

#36 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 16 November 2013 - 05:55 AM

View Posttheoriginalbit, on 15 November 2013 - 11:03 PM, said:

If you have references to the coroutines then yes you could resume it, but the way that most people use parallel.waitForAny means that once one function is completed the others will become suspended, and eventually killed and cleaned up by the Java Garbage Collector...
Worth to point out is that standard parallel don't give you access to those references so there is no way that i know for them to get resumed. So they will get cleaned up by Garbage Collector.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users