如何同步执行一个 Lwt 线程 [英] How to synchronously execute an Lwt thread

查看:66
本文介绍了如何同步执行一个 Lwt 线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法同步执行一个用Lwt库制作的线程?

Is there any way to synchronously execute a thread made with Lwt library?

具体来说,我正在尝试向服务器运行一系列发布请求,以计算某些值并返回结果.

To be specific, I am trying to run a series of post requests to a server that compute some value and returns a result.

基于对这个问题的回答:

Based on answers provided to this question:

如何在OCaml?

我可以使用低级方法(套接字)或使用 Cohttp 库来实现.低级方法的优点是直接和直接,但我宁愿坚持使用 Cohttp(不需要读/写循环等).不幸的是,我对 Lwt 完全陌生,并且基于:https://mirage.io/wiki/tutorial-lwt我知道从 ata 获得结果的唯一方法是运行 Lwt_main.run ,它假设在顶层调用不适合我.

I can make it with either low level approach (sockets) or using the Cohttp library. The low level approach has the advantage of being immediate and straightforward but I would rather stick to the Cohttp (no need for read/write loop etc.). Unfortunately I am completely new to the Lwt and based on: https://mirage.io/wiki/tutorial-lwt I understand that the only way to get result from a t to a is to run Lwt_main.run which is suppose to be invoked at top level which is not an option for me.

TL:DR

有什么办法可以得到结果:

Is there any way get a result from:

Client.get (Uri.of_string "http://localhost:8080/res")

不调用 Lwt_main.run 或为每个请求调用 Lwt_main.run(代码深处)还不错吗?

without calling Lwt_main.run or is calling Lwt_main.run for each request (deep inside a code) not so bad idea ?

推荐答案

您可以在程序内部调用 Lwt_main.run,前提是该调用未嵌套在对 Lwt_main 的外部调用中.run.听起来它不会嵌套在您的案例中,因此您可以在发出请求的地方调用它.

You can call Lwt_main.run deep inside the program, provided the call is not nested inside an outer call to Lwt_main.run. It sounds like it won't be nested in your case, so you can call it where you make the request.

这篇关于如何同步执行一个 Lwt 线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆