Closed failable closed 2 years ago
Hi @liebkne! See https://docs.rs/batched-fn/latest/batched_fn/#implementation-details, in particular:
When the
batched_fn
macro is invoked it spawns a new thread where the handler will be ran. Within that thread, every object specified in the context is initialized and then passed by reference to the handler each time it is run.
All three arguments (handler
, config
, and context
) are required, and yes, the code inside context
is only run once for initialization.
I hope that answers your question! I haven't used actix
recently, but if you do get an example working I'd be happy to share it in this repo.
@epwalsh
That's great help! Thank you very much! Following you hints, I spent some time to understand the macro and I think I've understood 90% of it. 😂
I will try to make a working example and once I did I will make a PR.
Great! Looking forward to it 🙂
I'm also pretty new to Rust and async things and found a tutorial from tokio-channel. After I read the source code I found the pattern (using channels) of the library is similar.
@epwalsh May I ask a question? Why Mutex
is needed in BatchedFn
?
@Yevgnen, great question! I'm glad you asked because it turns out it's actually not needed since flume::Sender
is Sync
(https://docs.rs/flume/latest/flume/struct.Sender.html#impl-Sync). https://github.com/epwalsh/batched-fn/pull/22 will remove the Mutex
.
Hi, thanks for this library!
I'm trying to use the library to write a demo using
rust-bert
andactix
. When I tried to put the model inbatched_fn!
, I got error likeNeed the model be
Sync
andSend
? I know there's arust-dl-webserver
project but I'm not quite understand the mechanism differences betweenactix
andwarp
as I'm pretty new to Rust. Can you provide an simpleactix
example or help me understand the usage withbatched_fn!
? e.g. How doescontext
,config
,handler
works? Are they all required? Is the code insidecontext
run only once for initialization (like loading the model)? Should one put other fields besidemodel
incontext
?Many thanks.