How to synchronously execute an Lwt thread - ocaml

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:
How do I make a simple GET request in OCaml?
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")
without calling Lwt_main.run or is calling Lwt_main.run for each request (deep inside a code) not so bad idea ?

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.

Related

Named Pipes on Windows

I am using named pipes on windows (C++). I was able to send data from one unrelated process to another process.
But for this I have to start the server first. and use "CreateNamedPipe" before I run the client. Client connects to the server using "CreateFile".
Is there a way I could run the client first before starting the server? (without trying to use the "CreateFile" inside a loop till it succeed)
Thank you.
IMO, it depends on your use case. My answer will be based on a case your software doesn't require the named pipe to work. For example, let's say a software which use a named pipe to log activities. This way we can understand your software can work perfectly without logging.
It should be possible if you start up your program without requiring the named pip to exist. Then, once everything is loaded up and functional, you could have sub-routine periodically checking for the named pipe existence (let's say every 5 seconds in order to not overload your CPU) and once created, you start using it.
Note: it will still looks like an infinite "a loop till it succeed" but I don't see anything wrong with that since you do it properly, says, you run it with non-blocking mechanism.
Note: it doesn't necessarily implies multi-process techniques. You can imagine a single main loop with a periodic checking (not every iteration).

Inject sleep() into a function of an external process

I know how to inject a DLL into a running process and also how to utilize functions used internally by the process e.g.
void__stdcall remoteMethod(unsigned short id)
{
typedef void (__stdcall *pFunctionAddress)(unsigned short);
pFunctionAddress pMyFunction = (pFunctionAddress)(0xCAFEBABE);
pMyFunction(id);
}
Now i want to add a sleep() into an existing method in the running process - this is the main loop of the program and doesnt stop for a sec and uses up all processing power.
I know that with frameworks like detours i could make a trampoline function which calls my function and then the original one - however my problem is that the while(1) loop is somewhere within the function of the external process. So i know the offset where the loop starts - and after that i would like to first call sleep() and then continue with the normal route of the loop.
The only alternative i saw so far is binary editing the program but this is not a good solution.
Any suggestion? Thanks
I think you are trying to be too cute here. Just call SuspendThread/ResumeThread alternately on a timer. I know it's ugly, but you aren't going to enter your solution in any beauty pageant I suspect.
Post the name of the spin-waiting program.
Wait for SO-ers to send hate mail to the developer.
Install the update the developer sends you as a bribe to stop the hate mail.
In principle, as long as you've been executed once within the space of the other process, and you know that the loop isn't executing, then you could enabling writing to text pages and patch the actual loop code in situ. You'll need a few redundant bytes to write a call to your function over (extending the function will need a lot of rewriting as all relative offsets will break).
This is not, however, terribly easy nor terribly robust. Consider why you want to to this, and if you can achieve the goal another way.

add curl_easy handles to working curl_multi_handle

I try to implement multi-threaded downloading using CURL library.
I prepare N threads (easy handles that download different ranges) and invoke
curl_multi_perform(multiHandle, &running)
after that.
My questions
how to check if specific thread (that was added to multi-handle) is downloading now? I haven't found any options.
If specific thread finishes downloading, It HAS to make connection AGAIN and continue downloading of another range. Is it possible to do?
The libcurl multi interface is not threaded. It does parallel transfers in the same thread!
You can add easy handles to the multi handle at any time you like. Just call curl_multi_perform() then and it'll drive all added easy handles. You can also remove handles at any time.
You should use curl_multi_info_read() to figure out which handles that have completed. Until they are completed, you can consider them in use. If you want to put a easy handle back to the multi handle to do another transfer, just remove it from the handle (possibly set new options) and add it again.
See also http://curl.se/libcurl/c/example.html for lots of libcurl examples, including a bunch that uses the multi interface. The general multi interface "tutorial" style docs is here: http://curl.se/libcurl/c/libcurl-multi.html

High Perfomance: Do call C++ methods directly from a python backend?

My Python backend (Django) has to request to a C++ library to get a result (with help of ctypes module).
Is it normal to call a C++ method directly? Or may be I need an intermediate thread manager that starts a new thread when python script wants a result?
Basically you have to decide what kind of operation flow you want. If you prefer synchronous processing you can call you method directly, if you favor asynchronous processing you will need an intermediate solution.
However, you have to be aware, that when you call the C++ routine directly form your Django app the call will end in the execution path that is triggered via the web application. If the processing takes more time than you want to wait, a job management system will be the better choice.
In any case I would recommend such a solution if the execution of your C++ routine takes too much time. You could then use polling to wait until the result is ready using e.g. Web Sockets.

XMLRPCPP asynchronously handling multiple calls?

I have a remote server which handles various different commands, one of which is an event fetching method.
The event fetch returns right away if there is 1 or more events listed in the queue ready for processing. If the event queue is empty, this method does not return until a timeout of a few seconds. This way I don't run into any HTTP/socket timeouts. The moment an event becomes available, the method returns right away. This way the client only ever makes connections to the server, and the server does not have to make any connections to the client.
This event mechanism works nicely. I'm using the boost library to handle queues, event notifications, etc.
Here's the problem. While the server is holding back on returning from the event fetch method, during that time, I can't issue any other commands.
In the source code, XmlRpcDispatch.cpp, I'm seeing in the "work" method, a simple loop that uses a blocking call to "select".
Seems like while the handling of a method is busy, no other requests are processed.
Question: am I not seeing something and can XmlRpcpp (xmlrpc++) handle multiple requests asynchronously? Does anyone know of a better xmlrpc library for C++? I don't suppose the Boost library has a component that lets me issue remote commands?
I actually don't care about the XML or over-HTTP feature. I simply need to issue (asynchronous) commands over TCP in any shape or form?
I look forward to any input anyone might offer.
I had some problems with XMLRPC also, and investigated many solutions like GSoap and XMLRPC++, but in the end I gave up and wrote the whole HTTP+XMLRPC from scratch using Boost.ASIO and TinyXML++ (later I swaped TinyXML to expat). It wasn't really that much work; I did it myself in about a week, starting from scratch and ending up with many RPC calls fully implemented.
Boost.ASIO gave great results. It is, as its name says, totally async, and with excellent performance with little overhead, which to me was very important because it was running in an embedded environment (MIPS).
Later, and this might be your case, I changed XML to Google's Protocol-buffers, and was even happier. Its API, as well as its message containers, are all type safe (i.e. you send an int and a float, and it never gets converted to string and back, as is the case with XML), and once you get the hang of it, which doesn't take very long, its very productive solution.
My recomendation: if you can ditch XML, go with Boost.ASIO + ProtobufIf you need XML: Boost.ASIO + Expat
Doing this stuff from scratch is really worth it.