I can't get my database access work with lwt. Should I include it in a thread? How? Or make a new thread which returns a 'a lwt
value? If so, what to do with that value?
The same goes for Printf.eprintf
, which also seems to be blocked by lwt. So I use Lwt_io instead. But why would lwt block regular io?
What I have is a simple db request like Db.update session
. It is within an Lwt_main.run main
function. All this is within a CGI script (should not matter, database access works fine until I start with the lwt commands).
I can give you more code if needed.
Regards
Olle
Edit
let main sock env =
(* code omitted *)
Gamesession.update_game_session env#db game_session_connected;
(* code omitted *)
Lwt_main.run (main sock_listen env)
Edit 2
This was the solution:
Lwt_preemptive.detach (fun () -> Db.call) ()
Printf.eprintf
is not "blocked", it's just that the buffering parameters are changed and often messages do not display before the end of the program. You should try eprintf "something\n%!"
(%!
means "flush"), but yes it's better to use Lwt_io
.
For the database, I don't know, you did not say which library you're using (at least the one called ocaml-mysql
is not Lwt-friendly, so it may require using Lwt_preemptive
).
Your:
Lwt_preemptive.detach (fun () -> Db.call) ()
This call creates a thread that, once executed, returns immediately the function Db.call
. So, basically in that case Lwt_preemptive.detach
does nothing :)
I don't know ocaml-mysql but if:
Db.call: connection_params -> connection_handle
you would have
let lwt_db_call connection_params =
Lwt_preemptive.detach Db.call connection_params