在C编程中使用libevent编写非阻塞事件 [英] write non-blocking event with with libevent in C programming

查看:335
本文介绍了在C编程中使用libevent编写非阻塞事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是libevent和套接字编程的新手,这就是为什么我对libevent如何作为异步和非阻塞工作有疑问. 这是参考代码. https://github.com/libevent/libevent/blob/master /sample/http-server.c

I am newbie in libevent and socket programming that's why I have question about how libevent working as asynchronous and non blocking. Here is the reference code. https://github.com/libevent/libevent/blob/master/sample/http-server.c

static void dump_request_cb(struct evhttp_request *req, void *arg)
{
    const char *cmdtype;
    struct evkeyvalq *headers;
    struct evkeyval *header;
    struct evbuffer *buf;

    printf("Request Start\n");
    sleep(30); // delay to read request to check non blocking event.

    switch (evhttp_request_get_command(req)) {
    case EVHTTP_REQ_GET: cmdtype = "GET"; break;
    case EVHTTP_REQ_POST: cmdtype = "POST"; break;
    case EVHTTP_REQ_HEAD: cmdtype = "HEAD"; break;
    case EVHTTP_REQ_PUT: cmdtype = "PUT"; break;
    case EVHTTP_REQ_DELETE: cmdtype = "DELETE"; break;
    case EVHTTP_REQ_OPTIONS: cmdtype = "OPTIONS"; break;
    case EVHTTP_REQ_TRACE: cmdtype = "TRACE"; break;
    case EVHTTP_REQ_CONNECT: cmdtype = "CONNECT"; break;
    case EVHTTP_REQ_PATCH: cmdtype = "PATCH"; break;
    default: cmdtype = "unknown"; break;
    }

    printf("Received a %s request for %s\nHeaders:\n",
        cmdtype, evhttp_request_get_uri(req));

    headers = evhttp_request_get_input_headers(req);

    for (header = headers->tqh_first; header;
       header = header->next.tqe_next) {
            printf("  %s: %s\n", header->key, header->value);
    }
    buf = evhttp_request_get_input_buffer(req);

    puts("Input data: <<<");

    while (evbuffer_get_length(buf)) {
            int n;
            char cbuf[128];
            n = evbuffer_remove(buf, cbuf, sizeof(cbuf));
            if (n > 0)
                    (void) fwrite(cbuf, 1, n, stdout);
    }
    puts(">>>");

    evhttp_send_reply(req, 200, "OK", NULL);
}

我在上述请求中创建了30秒的延迟.

I creating delay of 30 secs in above request.

当我从浏览器发送两个请求时.该代码应立即开始同时服务两个请求.但这是没有发生的.真正的情况是第二个请求在完成第一个请求后才有30秒的延迟.这意味着总共需要60秒才能满足两个请求.

When I send two request from browser. The code should immediately start serving two request at a time. but this is not happened. The real scenario is that second request serve after completion of first request with 30 secs delay. it means it takes total of 60 secs to served two requests.

任何人都可以告诉我它如何作为非阻塞方式工作.

So could anyone tell me how it works as non_blocking.

推荐答案

sleep(30) 您拨打的电话正在阻塞,libevent并没有采取任何黑魔法来阻止执行阻塞电话.
您需要小心并且仅使用非阻塞API.在这种情况下,您希望将响应延迟30秒,因此可以很容易地使用libevent的evtimer_add().但是,同样的原理也适用于您要使用的任何类型的api,您必须以非阻塞方式使用它们(文件读取,对其他服务器的调用,数据库访问等).

The sleep(30) call you made, is blocking, libevent doesn't do any black magic to prevent you from performing blocking calls.
You need to be careful and use only non-blocking APIs. In this case you want to delay response for 30 seconds, so easy enough you can use libevent's evtimer_add() . But this same principle applies to any kind of api you want to use, you must use them in non blocking way (file reads, calls to other servers, DB access, etc).

这篇关于在C编程中使用libevent编写非阻塞事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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