我是libevent和socket編程的新手,這就是爲什麼我有關於libevent如何作爲異步和非阻塞工作的問題。 以下是參考代碼。 https://github.com/libevent/libevent/blob/master/sample/http-server.c在C編程中用libevent編寫非阻塞事件
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秒的延遲。
當我從瀏覽器發送兩個請求。代碼應該立即開始一次提供兩個請求。但這沒有發生。真正的情況是,第二次請求在第一次請求完成之後以30秒的延遲進行服務。這意味着總共需要60秒來處理兩個請求。
所以任何人都可以告訴我它是如何工作的non_blocking。
我不知道'libevent',但我懷疑你混合'非阻塞'與'並行' – MeNa
我開始網絡編程與libevent因爲它說你不需要線程,它是非阻塞異步套接字讀取。但我無法執行它。此外,我沒有找到任何好的例子在網絡上用libevent編寫非阻塞代碼。 – Vikas
您可以使用循環和非阻塞套接字來獲得異步服務器(性能糟糕),但是當您** sleep()**系統時 - 循環再次被阻塞!你必須並行! – MeNa