Jump to content




[Tutorial] Parallel API


35 replies to this topic

#1 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 08 June 2012 - 07:32 AM

What is the Parallel API?
The parallel api allows you to run many functions at once. It checks for any functions listed in the line of code. It also allows sequencing.

Syntax
parallel.waitForAny(func1, func2, etc.)
parallel.waitForAll(func1, func2, etc.)

How these work
As you can see, waitForAny would wait for any to queue.
waitForAll is for sequencing, it waits for everything to complete.

Why is it helpful?
It helps because it doesn't require lots of lines of code to run 2 functions at one time. It was specifically made for multitasking.

How to use it properly
Say you have a function that you want it to receive all incoming messages.
function recv()
  while true do
	  id, msg = rednet.receive()
	  print(msg)
  end
end


Now you have another function that sends rednet.
function send()
  while true do
	  write("> ")
	  input = read()
	  rednet.broadcast(input)
  end
end

Now if you are lazy like 75% of all Americans (such obesity, it disgusts me.) You won't want to make a bunch of lines of code making it recv and send at the same time. This is what parallel prevents.
while true do
  parallel.waitForAny(recv, send)
end

It simply runs the functions simultaneously!

#2 Pinkishu

  • Members
  • 484 posts

Posted 08 June 2012 - 10:17 AM

 Noodle, on 08 June 2012 - 07:32 AM, said:

Now if you are lazy like 75% of all Americans (such obesity, it disgusts me.)
*facepalm*

 Noodle, on 08 June 2012 - 07:32 AM, said:

It simply runs the functions simultaneously!

Actually it runs them in turns till one of them finishes then the other is terminated (ofc that never happens cause of the while loops)

#3 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 08 June 2012 - 07:10 PM

@Pinkishu
xD (facepalm)
Saying, "It simply runs the functions simultaneously!" That was meant for people who are starters to lua.

#4 kazagistar

  • Members
  • 365 posts

Posted 08 June 2012 - 08:46 PM

Lying about abstractions is bad, because they always leak through.

#5 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 09 June 2012 - 03:29 AM

 Pinkishu, on 08 June 2012 - 10:17 AM, said:

 Noodle, on 08 June 2012 - 07:32 AM, said:

Now if you are lazy like 75% of all Americans (such obesity, it disgusts me.)
*facepalm*

 Noodle, on 08 June 2012 - 07:32 AM, said:

It simply runs the functions simultaneously!

Actually it runs them in turns till one of them finishes then the other is terminated (ofc that never happens cause of the while loops)

But it doesn't run the two functions in turn, it alternates between the instructions inside. Right?

#6 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 09 June 2012 - 03:59 AM

@Orwell
No it doesn't alternate. Instead it runs the simultaneously (same time). <- Basic version

#7 Pinkishu

  • Members
  • 484 posts

Posted 09 June 2012 - 10:22 AM

 Orwell, on 09 June 2012 - 03:29 AM, said:

 Pinkishu, on 08 June 2012 - 10:17 AM, said:

 Noodle, on 08 June 2012 - 07:32 AM, said:

Now if you are lazy like 75% of all Americans (such obesity, it disgusts me.)
*facepalm*

 Noodle, on 08 June 2012 - 07:32 AM, said:

It simply runs the functions simultaneously!

Actually it runs them in turns till one of them finishes then the other is terminated (ofc that never happens cause of the while loops)

But it doesn't run the two functions in turn, it alternates between the instructions inside. Right?

Well it doesn't alternate between instructions.
It runs Function A, then if function A yields it runs Function B, then if Function B yields it runs Function A etc

"yielding" is done when the coroutine itself calls coroutine.yield() many functions seem to do so internally (e.g. pullEvent, sleep, etc)

#8 matejdro

  • Members
  • 324 posts

Posted 09 June 2012 - 12:10 PM

So, when one function yields, it will run second function until second one yields, then again first one and so on?

Thanks for explaining this , wiki documentation on this subject is IMHO very bad.

#9 Pinkishu

  • Members
  • 484 posts

Posted 09 June 2012 - 01:24 PM

Yeah it runes the first subroutine, then the second once the first yields and so on until its through all, then it restarts at the first

#10 kazagistar

  • Members
  • 365 posts

Posted 10 June 2012 - 02:15 AM

If one of the subroutines calls a yield that waits for a os message that does not come, or they are both waiting for an os message and steal it from the other, the abstraction starts breaking down very fast.

#11 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 10 June 2012 - 07:53 AM

The way the parralel API works though, it won't "steal" the message from the other. Both coroutines will get the same message regardless of what the other does with it.

As for waiting for a message that does not come (I assume a os.pullEvent with an argument) I'm not sure but doesn't the parallel API keep sending events to the one which isn't listening for the specific event?

#12 kazagistar

  • Members
  • 365 posts

Posted 10 June 2012 - 08:07 PM

Right, nevermind, I don't know what I was thinking, I looked over the code again and it makes sense.

#13 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 11 June 2012 - 04:31 PM

@kazagistar
My code, incorrect? PFFT. Don't insult me.
(JK my code is sometimes incorrect, we all make mistakes)

#14 kazagistar

  • Members
  • 365 posts

Posted 11 June 2012 - 04:44 PM

I wasn't talking about your code, I was talking about the parallel API library.

#15 Bossman201

  • New Members
  • 92 posts

Posted 11 June 2012 - 11:26 PM

So does parallel only work one time unless you have a loop?

#16 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 13 June 2012 - 04:21 AM

@Bossman201
Yes.

#17 Darky_Alan

  • Members
  • 89 posts
  • LocationPuerto Rico

Posted 24 July 2012 - 08:28 AM

Wouldn't this just jump between functions till one of them does something? Don't really feel the simultanious vibe, more of a jumping loops.

"Oh hey have you heard your signal yet?"

"No, it's your turn to listen for yours, I'll take an extremely short break."

And vice versa?

#18 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 24 July 2012 - 08:30 AM

Nope, simultaneous. Keeps them both alive.
-- I think it just does it so fast that it won't exit or its using sockets.

#19 Darky_Alan

  • Members
  • 89 posts
  • LocationPuerto Rico

Posted 24 July 2012 - 08:31 AM

 Noodle, on 24 July 2012 - 08:30 AM, said:

-- I think it just does it so fast that it won't exit or its using sockets.

Kinda what I meant by "an extremely short break".

What are sockets?

#20 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 24 July 2012 - 08:34 AM

^ 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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users