0

The problem

  • One data source generating data in format {key, value}
  • Multiple receivers each waiting for different key

Example

Getting data is run in loop. Sometimes I will want to get next value labelled with key by using

Value = MyClass:GetNextValue(Key)

I want my code to stop there until the value is ready (making some sort of future(?) value). I've tried using simple coroutines, but they work only when waiting for any data.

So the question I want to ask is something like How to implement async values in lua using coroutines or similar concept (without threads)?

Side notes

The main processing function will, apart from returning values to waiting consumers, process some of incoming data (say, labeled with special key) itself.

The full usage context should look something like:

-- in loop
    ReceiveData()
    ProcessSpecialData()
--

-- Called outside the loop:
V = RequestDataWithGivenKey(Key)
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135

1 Answers1

2

How to implement async values

You start by not implementing async values. You implement async functions: you don't get the value back until has been retrieved.

First, your code must be in a Lua coroutine. I'll assume you understand the care and feeding of coroutines. I'll focus on how to implement RequestDataWithGivenKey:

function RequestDataWithGivenKey(key)
  local request = FunctionThatStartsAsyncGetting(key)
  if(not request:IsComplete()) then
    coroutine.yield()
  end

  --Request is complete. Return the value.
  return request:GetReturnedValue()
end

FunctionThatStartsAsyncGetting returns a request back to the function. The request is an object that stores all of the data needs to process the specific request. It represents asking for the value. This should be a C-function that starts the actual async getting.

The request will be either a userdata or an encapsulated Lua table that stores enough information to communicate with the C-code that's doing the async fetching. IsComplete uses the internal request data to see if that request has completed. GetReturnedValue can only be called when IsComplete returns true; it puts the value on the Lua stack, so that this function can return it.

Your external code simply needs to handle the async stuff internally. Between resumes of these Lua coroutines, you'll need to pump whatever async stuff is doing the fetching, if there are outstanding requests.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Thanks for the answer. I'd like to ask, however, if it's possible to still have lua shell with whole code packed in coroutine, as you wrote? I know it can be done with threads, but I'm just wondering if it's possible w/o them. – Bartek Banachewicz May 20 '12 at 12:50
  • @Bartek: Is it *possible*? It depends on a lot of stuff you haven't said. What is the nature of the async access? How are you accessing it? And so forth. – Nicol Bolas May 20 '12 at 14:20