a2svior
a2svior
MModular
Created by a2svior on 7/15/2024 in #questions
SSL/TLS Support?
Is there anything planned for Mojo stdlib in terms of SSL/TLS? I'm thinking something like Golang's crypto/tls or Python's SSL socket wrapper We want to add HTTPS support to Lightbug but for that would be good to know if the stdlib will include this functionality any time soon or if this has to be built by the community
42 replies
MModular
Created by a2svior on 5/11/2024 in #questions
Passing a trait as a parameter to `Dict`?
This is related to a proposal for adding Middleware functionality to Lightbug HTTP: https://github.com/saviorand/lightbug_http/pull/33#issuecomment-2105547981 We're trying to do something like the following:
## HTTPHandler is an interface for handling HTTP requests in the RouterMiddleware.
## It is a leaf node in the middleware chain.
trait HTTPHandler:
fn handle(self, context: Context) -> HTTPResponse:
...

## Router middleware routes requests to different middleware based on the path.
@value
struct RouterMiddleware(Middleware):
var next: Middleware
var routes: Dict[String, HTTPHandler]
## HTTPHandler is an interface for handling HTTP requests in the RouterMiddleware.
## It is a leaf node in the middleware chain.
trait HTTPHandler:
fn handle(self, context: Context) -> HTTPResponse:
...

## Router middleware routes requests to different middleware based on the path.
@value
struct RouterMiddleware(Middleware):
var next: Middleware
var routes: Dict[String, HTTPHandler]
Where routes: Dict[String, HTTPHandler] takes in an HTTPHandler trait which can be implemented by different handlers, including ones created by the user. Currently, since Dict expects a value that implements CollectionElement, so we're getting the following error:
middleware/router.mojo:15:36: error: 'Dict' parameter #1 has 'CollectionElement' type, but value has type 'AnyTrait[HTTPHandler]'
self.routes = Dict[String, HTTPHandler]()
middleware/router.mojo:15:36: error: 'Dict' parameter #1 has 'CollectionElement' type, but value has type 'AnyTrait[HTTPHandler]'
self.routes = Dict[String, HTTPHandler]()
This persists even if HTTPHandler extends CollectionElement like so: trait HTTPHandler(CollectionElement). Any ideas on how we can get this to work?
4 replies
MModular
Created by a2svior on 4/5/2024 in #questions
Async function docs?
Hi! I found a mention of async fn and async def in the roadmap document, but no description of how these actually work? Does Mojo support any sort of async/await workflow? The only mention of async I see is in the changelog and just a tiny bit in the code of the coroutine in the standard library. Can we have better documentation for this?
5 replies
MModular
Created by a2svior on 1/3/2024 in #questions
Are coroutines executed in parallel?
I'm trying this example:
fn main():
async fn my_async_fn() -> Int:
sleep(2)
return 5

fn my_sync_fn():
let my_coro: Coroutine[Int] = my_async_fn()
print(my_coro())

fn call_em_together():
_ = my_sync_fn()
_ = my_sync_fn()

fn call_em_coroutine():
let my_coro_1: Coroutine[Int] = my_async_fn()
print(my_coro_1())
let my_coro_2: Coroutine[Int] = my_async_fn()
print(my_coro_2())

# fn call_em_parallel():
# parallelize[my_sync_fn](2)

let ns = time_function[call_em_together]() # do the same for other two
print(ns)
fn main():
async fn my_async_fn() -> Int:
sleep(2)
return 5

fn my_sync_fn():
let my_coro: Coroutine[Int] = my_async_fn()
print(my_coro())

fn call_em_together():
_ = my_sync_fn()
_ = my_sync_fn()

fn call_em_coroutine():
let my_coro_1: Coroutine[Int] = my_async_fn()
print(my_coro_1())
let my_coro_2: Coroutine[Int] = my_async_fn()
print(my_coro_2())

# fn call_em_parallel():
# parallelize[my_sync_fn](2)

let ns = time_function[call_em_together]() # do the same for other two
print(ns)
call_em_together() and call_em_coroutine() both print roughly 4 seconds as the result. call_em_parallel() prints roughly 2 seconds. Does that mean coroutines are not executed in parallel? Is there a way to execute them in parallel, like with goroutines in Go? I saw a TaskGroup mentioned in the changelog but no documentation for it exists yet, and I'm not sure how to import it. My use case is to have a blocking process/an infinite loop (e.g. a server listener) running on one coroutine , and another processes (e.g. a client making requests) executing in another in parallel
4 replies