Closed panegyrize closed 6 months ago
Is the two-thread-mode a suggested way for my case?
Yes. And future more, you can create a thread pool to do the transcoding work.
If so, the question now becomes: how can I pass the frames between the two threads so the newStreamResponse can pick up the data and use it inside the callback function.
You can pass everything to your working thread when you receive the request, and do everything there. In this case you dont need to worry about synchonizing problems.
# pseudo code
void Controller::method(const HttpRequestPtr & req, Callback && callback, std::string && video)
{
workingThreadPool->queue([req, callback = std::move(callback), video = std::move(video)](){
...
});
}
Thanks for the response, @hwc0919.
Is this workingThreadPool
shared among all the threads that I launch in the main loop function e.g., drogon::app().setThreadNum(12);
? Or is it tied to the single thread which runs the heavy workload?
Is this workingThreadPool shared among all the threads that I launch in the main loop function e.g., drogon::app().setThreadNum(12);? Or is it tied to the single thread which runs the heavy workload?
The latter, but not exactly (it's your own choice to use single or multiple threads).
drogon::app().setThreadNum(12)
sets the number of IO loops, which you don't want to use for cpu intensive workload, because it will block subsequent requests.
CPU intensive workload should be processed in standalone thread(s). You can use trantor::EventLoopThreadPool
, trantor::ConcurrentTaskQueue
, trantor::SerialTaskQueue
or your own implementations as your working threadpool. The number of threads should depends on your workload.
Hi,
I am using Drogon to build a RESTful service which receives requests from clients to transcode the video files. The server needs to stream the new video frames back to the client. Note that I will not save the new video as a file and stream the file back, but I want to stream the video buffer on the fly.
At the time of writing this post, I have successfully registered the methods and implemented the core logic of the transcoding part. I also plan to use the
newStreamResponse
to stream the video buffer back to the client. I have been blocked by several issues on this implementation and would appreciate any comments and suggestions on this post.newStreamResponse
can pick up the data and use it inside the callback function? Currently, I use a buffer which is a typical producer-consumer buffer, i.e., guarded by a mutex, to transfer the frames. But the I/O thread is blocked by waiting the buffer to be filled in. Here is a code snippet that describes my service.void DrogonController::video(const drogon::HttpRequestPtr& request, std::function<void(const drogon::HttpResponsePtr&)>&& callback, const std::string& videoSrcFile) { // Parse the request parameters to find path, other parameters, etc. parse(); // Run the transcode in another thread. workerThread_.getLoop()->queueInLoop([&sync_buffer]{ // This function gets queued for execution
video.transcode(); pushFrameToBuffer(sync_buffer); });
}