When using functions from addon blocks the main process node js?

Đang mở
#77 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
cpp, node.js
Lĩnh vực
backend

Hướng nghiên cứu

Start with the readDataSerial N-API wrapper shown in the issue and trace the blocking Ibox_Controller_ReadData call. Review the Node.js addon guidance for asynchronous work, then determine how the result is delivered without blocking the main process. Done means the long-running read no longer prevents the Node.js main thread from processing other work.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Implemented a wrapper for the function using N-API. It works, but the problem is that when it works the main thread of the node does not work? How can I rewrite this function so that it does not block the main thread? What do I need to use async, callback, thread for this?

napi_value readDataSerial(napi_env env, napi_callback_info info) {
        napi_status status;
        size_t argc = 1;    
        napi_value args[1], object;  
        napi_value id, data;
        status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
        assert(status == napi_ok);
        if (argc < 1) {
          napi_throw_type_error(env, NULL, "Wrong number of arguments");
          return NULL;
        }    
        //....
        Ibox_ReadData *context = calloc(1, sizeof(Ibox_Context));
        //this function takes a long time and blocks the main process
        Ibox_Result_Submit *submitResult = Ibox_Controller_ReadData(context);
        status = napi_create_object(env, &object);
        status = napi_create_string_utf8(env, submitResult->id, NAPI_AUTO_LENGTH, &id);
        status = napi_set_named_property(env, object, "id", id);         
        status = napi_create_string_utf8(env, submitResult->date, NAPI_AUTO_LENGTH, &date);
        status = napi_set_named_property(env, object, "data", data);         
        assert(status == napi_ok);
        return object;
    })  
Ngôn ngữ chính
C++
Star
2.6k
Fork
602
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của nodejs/node-addon-examples

Tất cả issue của nodejs/node-addon-examples

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.