add some spaghetty code that tries using async for reading
This commit is contained in:
parent
b934e339c2
commit
6e5e51bd2c
@ -28,8 +28,7 @@ Problems:
|
||||
if it's safe to call write or not.
|
||||
|
||||
2. Default lua `io` module does not provide ways to make the read()
|
||||
from fifo non-blocking. Makes it less useful, but maybe still
|
||||
possible to use with async environment?
|
||||
from fifo non-blocking.
|
||||
|
||||
Solutions:
|
||||
----------
|
||||
@ -50,3 +49,6 @@ Solutions:
|
||||
```
|
||||
|
||||
It just ignores `SIGPIPE`, which should be fine to do.
|
||||
|
||||
|
||||
2. It's possible to do `read()` in async environment.
|
||||
|
62
init.lua
62
init.lua
@ -1,4 +1,6 @@
|
||||
|
||||
local message_count = 0
|
||||
|
||||
local PATH = minetest.get_worldpath()
|
||||
|
||||
local OUTPUT_FILENAME = PATH .. DIR_DELIM .. "mt_output"
|
||||
@ -10,26 +12,74 @@ local output_fifo
|
||||
|
||||
local function send_data()
|
||||
if not output_fifo then
|
||||
-- This is to delay opening until we need it, because open()
|
||||
-- blocks until other side is opened too.
|
||||
minetest.log(string.format("* MT Opening FIFO at %s", OUTPUT_FILENAME))
|
||||
output_fifo = io.open(OUTPUT_FILENAME, 'wb')
|
||||
end
|
||||
local data = string.format("blah %f\n", os.clock())
|
||||
local data = string.format("%04d blah %f\n", message_count, os.clock())
|
||||
message_count = message_count + 1
|
||||
minetest.log(string.format("* MT Sending data: '%s'", data))
|
||||
local res = output_fifo:write(data)
|
||||
--print("write res", res)
|
||||
output_fifo:flush() -- if not flushed, the write will be buffered
|
||||
end
|
||||
|
||||
local function receive_data()
|
||||
|
||||
local function receive_data(input_filename)
|
||||
-- this function will block
|
||||
local data
|
||||
local input_fifo
|
||||
if not input_fifo then
|
||||
input_fifo = io.open(INPUT_FILENAME, 'rb')
|
||||
-- This is to delay opening until we need it, because open()
|
||||
-- blocks until other side is opened too.
|
||||
input_fifo = io.open(input_filename, 'rb')
|
||||
end
|
||||
if input_fifo then -- open can fail if server started first
|
||||
local data = input_fifo:read("*line")
|
||||
minetest.log(string.format("* MT Received data: '%s'", data))
|
||||
data = input_fifo:read("*line")
|
||||
minetest.log(string.format("* ASYNC Received data: '%s'", data))
|
||||
end
|
||||
return data
|
||||
end
|
||||
|
||||
|
||||
local async_running = false
|
||||
local async_result
|
||||
local function start_async_recv()
|
||||
assert(not async_running, "ERROR: Async is already running")
|
||||
local function async_callback(data)
|
||||
async_running = false
|
||||
if data == nil then
|
||||
minetest.log("* Async call returned nil: FIFO was closed")
|
||||
end
|
||||
async_result = data
|
||||
end
|
||||
minetest.handle_async(receive_data, async_callback, INPUT_FILENAME)
|
||||
async_running = true
|
||||
end
|
||||
|
||||
|
||||
local function poll_result()
|
||||
-- TODO use queue?
|
||||
if async_running then
|
||||
return nil
|
||||
end
|
||||
local res = async_result
|
||||
async_result = nil
|
||||
start_async_recv() -- get ready for the next poll
|
||||
return res
|
||||
end
|
||||
|
||||
|
||||
local function try_to_recv()
|
||||
print("* Polling...")
|
||||
local res = poll_result()
|
||||
if res then
|
||||
minetest.log(string.format("* MT Received data: '%s'", res))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local timer = 0
|
||||
local UPDATE_INTERVAL = 2.123
|
||||
local function step(dtime)
|
||||
@ -37,7 +87,7 @@ local function step(dtime)
|
||||
if timer > UPDATE_INTERVAL then
|
||||
timer = timer - UPDATE_INTERVAL
|
||||
send_data()
|
||||
receive_data()
|
||||
try_to_recv()
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -25,6 +25,8 @@ def create_fifo():
|
||||
def cleanup():
|
||||
print("* Cleaning up")
|
||||
os.unlink(INPUT_FILENAME)
|
||||
if output_fifo:
|
||||
print("* WARN: output_fifo was never opened")
|
||||
output_fifo.close()
|
||||
os.unlink(OUTPUT_FILENAME)
|
||||
|
||||
@ -37,6 +39,10 @@ def process_data(data):
|
||||
print(f"* Opening '{OUTPUT_FILENAME}'...")
|
||||
output_fifo = open(OUTPUT_FILENAME, 'w')
|
||||
print(f"* Writing data to '{OUTPUT_FILENAME}'")
|
||||
|
||||
# simulate some work
|
||||
import time; time.sleep(3)
|
||||
|
||||
output_fifo.write(data.upper())
|
||||
# output_fifo.write('\n')
|
||||
output_fifo.flush() # must flush
|
||||
|
Loading…
Reference in New Issue
Block a user