hiredis Redis库是否为异步回调创建自己的线程 [英] Does the hiredis Redis library create its own thread for async callbacks

查看:757
本文介绍了hiredis Redis库是否为异步回调创建自己的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多线程环境中使用Redis,并且有一个如何操作的问题。我在我的c ++应用程序中使用hiredis c库。



我的问题是这样的:如果我使用异步模式,当一个回调被触发,回调将在Redis客户端创建的另一个线程中处理?正如将在创建调用的线程不受回调的处理的影响?感谢!

解决方案

Redis客户端不会创建任何额外的隐藏线程,并且可以在现有线程中工作。



Redis在另一个(主)进程中工作。您使用的redis API在本地进程中工作,并使用进程间通信到主进程。异步请求意味着你的进程或线程将任务放到另一个,然后可以做任何其他yout任务或等待事件。有一段时间后,异步回复到达您的应用程序并可供使用。您的应用程序必须使用 https://en.wikipedia.org/wiki/Event_loop 或任何异步管理系统通过调用回调处理事件(在这种情况下为redis回答)通知您。



异步架构意味着您运行事件循环,为每个事件调用回调处理程序。当回调调用时,可以创建多个异步任务。一旦创建任务,它应该比任务完成或错误发生时将调用回调事件处理程序。回调可能会分配到启动应用程序或新的Web连接出现。当回调调用时,可以创建redis任务,稍后的结果事件回调将被调用。所有的东西在当前线程。



Redis的单线程性质: http://redis.io/topics/latency#single-threaded-nature-of-redis

由于Redis使用复用和非阻塞I / O,因此客户端套接字处于非阻塞状态。 http://redis.io/topics/clients 这表示您的客户端永远不会被阻止。



Redis 2.4中的线程仅用于在后台执行一些缓慢的I / O操作,主要与磁盘I / O相关,但这不会改变事实Redis使用单个线程来处理所有请求。


I am using Redis in a multithreaded environment, and have a question as to how it operates. I am using the hiredis c library in my c++ application.

My question is this: If I am using the asynchronous mode when a callback is fired will the callback be handled in another thread created by the Redis client? As in will the thread where the call was created not be affected by the handling of the callback? Thanks!

解决方案

Redis client does not create any additional clent threads, and works in existing thread.

The Redis worked in another (main) process. The redis API which you use worked in your local process and use interprocess communication to main process. Async request mean that your process or thread put task to another, and after that may do any other yout task or wait event. Some time later async reply arrived to your application and available for use. You application must use https://en.wikipedia.org/wiki/Event_loop or any async management system that notify you by calling callback to handle event (in this case redis answer).

Asyncronous architecture mean that you runing event loop that call callback handlers for each event. When callback called you can create number of asynchronous tasks. Once task created, it supposed than when task done or error occur the callback event handler will be called. Callback may be assigned to start app or to new web connection appear. When callback called you can create the redis task, and later result event callback will be called. All things in current thread. I нou have multiple thread, reasonable to expect that you have one event loop per each thread.

Single threaded nature of Redis: http://redis.io/topics/latency#single-threaded-nature-of-redis

The client socket is put in non-blocking state since Redis uses multiplexing and non-blocking I/O. http://redis.io/topics/clients This mean that your client will never be blocked.

From Redis 2.4 threads in Redis used only in order to perform some slow I/O operations in the background, mainly related to disk I/O, but this does not change the fact that Redis serves all the requests using a single thread.

这篇关于hiredis Redis库是否为异步回调创建自己的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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