wahern / cqueues

Continuation Queues: Embeddable asynchronous networking, threading, and notification framework for Lua on Unix.
http://25thandclement.com/~william/projects/cqueues.html
MIT License
244 stars 37 forks source link

can you help me? #220

Closed otgo closed 5 years ago

otgo commented 5 years ago

Im trying to do a async process, this is my testing script:

local cqueues = require 'cqueues'
local proc_functions = cqueues.new()
local proc_check = cqueues.new()
local function proc1()
    while true do
        proc_functions:loop()
    end
end
local function proc2()
    while true do
        local inpt = io.stdin:read()
        proc_functions:wrap(function()
            print("HELLO")
        end)
        -- proc_functions:loop() -- if I uncomment this, it works
    end
end
proc_check:wrap(proc1)
proc_check:wrap(proc2)
proc_check:loop()

Problem here is, i want to check if there are new functions, and execute it in "proc1"

How do I do that?

daurnimator commented 5 years ago
local inpt = io.stdin:read()

This is blocking. You'll need to wrap it in a cqueues-yielding read operation. The easiest way to do so is:

local cs = require "cqueues.socket"
local stdin = cs.fdopen(0)
--- setup code here
    local inpt = stdin:read()
--- etc.
otgo commented 5 years ago

but if, in my code im not using stdin? It is only an example

otgo commented 5 years ago

Example:

local cqueues = require 'cqueues'
local proc_functions = cqueues.new()
local proc_check = cqueues.new()
local function proc1()
    while true do
        proc_functions:loop()
    end
end
local function proc2()
    while true do
           https = require 'ssl.https'
           https.request("nnnn")
            print("HELLO")
        end)
        -- proc_functions:loop() -- if I uncomment this, it works
    end
end
proc_check:wrap(proc1)
proc_check:wrap(proc2)
proc_check:loop()
daurnimator commented 5 years ago

https.request("nnnn")

That is also a blocking operation. See lua-http instead.

otgo commented 5 years ago

so, how could I do it with a ssl request?

RyanSquared commented 5 years ago

lua-http is a library that integrates properly with cqueues and allows for it to perform operations without blocking up your client. You can change your example to, instead of using require 'ssl.https', instead require 'http.request as provided by the library I mentioned. Examples on how to use an HTTP client with that library should exist on that link.

In addition to that, you're not checking errors returned from the proc_functions loop, so you should wrap the loop function call in an assert() or use some other method to handle the error.

otgo commented 5 years ago

okaay :3 thanks for it, i will answer there to make my request sucessfully

otgo commented 5 years ago

I've replicated that i want to do:

local cqueues = require 'cqueues'
local cop2 = cqueues.new()
local thread = cqueues.new()
thread:wrap(function() print("PRINT TESTING") end)
cop2:wrap(function()
    while true do
        assert(thread:step())
    end
end)
cop2:wrap(function()
    while true do
        --_some_function_()
    end
end)
assert(cop2:loop())
otgo commented 5 years ago

This is a flowchart:

initialization

daurnimator commented 5 years ago

Do you still have an open question here? I'm not sure I see a question any more...

otgo commented 5 years ago

ahmm...

otgo commented 5 years ago

I mean, i want to do something like that