@dave1707 Thanks for your recent post - it did get me over a few obstacles in my thinking - especially your done
variable. @se24vad, also - thank you, though I still have some issues with the code you posted. If I flesh it out with an exec and thread_update functions (from your previous code, then the print order is still not what I expect/hope for. Here’s the full code:
local thread_queue = {}
local function exec(func, ...)
local params = {...}
local thread = function(self) func(self, unpack(params)) end
table.insert(thread_queue, coroutine.create(thread))
end
local function thread_update()
if #thread_queue > 0 then
if coroutine.status(thread_queue[1]) == "dead" then table.remove(thread_queue, 1)
else coroutine.resume(thread_queue[1], thread_queue[1]) end
end
end
function draw()
thread_update()
end
local function success(response)
while not response do -- block until data from http request comes in
coroutine.yield()
end
print(response) -- when data arrived, processing it
print("response received. done.")
end
local function request()
http.request("http://google.com/home", success)
print("request sent. wait...")
end
local function continue_with_some_other_stuff()
print("now that we completed the request and processed the response, it's time to continue with the rest of the queue")
end
function setup()
request() -- start request
exec(success) -- create a queue to work through
exec(continue_with_some_other_stuff)
end
Here’s the response order I get:
request sent. wait...
thread: 0x1c01bb9e8
response received. done.
now that we completed the request and processed the response, it's time to continue with the rest of the queue
<!DOCTYPE html>
<title>Google</title>
<script>
var url = 'https://madeby.google.com/home/';
url += window.location.search;
url += window.location.hash;
window.location = url;
</script>
response received. done.
Reflection:
The response data (arbitrary google.com request) comes back after the exec() calls are made. In order words, when Insee “Response received. Done.”, I would hope to have the google data already printed.
The exec(success) call causes the thread ID to print, again before getting the google data.
The goal is for the print statements to be in this order:
- request sent
- google data prints
- response received
- continue
Thanks for everyone throwing code my way! Every snippet helps.
In fact, that’s the challenge. (“Programming challenge! everyone join in!”). I would love to see the shortest code snippet (just the essentials, so its easy to see the blocking logic) that can:
- print a starting message
- make a request and print the response data when received (I’ve been using google html data, for no reason - its arbitrary)
- Only after step 2, print an ending message
Nothing else. 3 MUST wait until 2 prints its response data.
The drawing or other messages in some of the examples makes things more interesting, but also more complex and clouds the essential blocking logic. I just want to understand steps 1 - 3 in their purest form.
Thanks again.