Blame docs/src/threadpool.rst

Packit Service 7c31a4
Packit Service 7c31a4
.. _threadpool:
Packit Service 7c31a4
Packit Service 7c31a4
Thread pool work scheduling
Packit Service 7c31a4
===========================
Packit Service 7c31a4
Packit Service 7c31a4
libuv provides a threadpool which can be used to run user code and get notified
Packit Service 7c31a4
in the loop thread. This thread pool is internally used to run all file system
Packit Service 7c31a4
operations, as well as getaddrinfo and getnameinfo requests.
Packit Service 7c31a4
Packit Service 7c31a4
Its default size is 4, but it can be changed at startup time by setting the
Packit Service 7c31a4
``UV_THREADPOOL_SIZE`` environment variable to any value (the absolute maximum
Packit Service 7c31a4
is 1024).
Packit Service 7c31a4
Packit Service 7c31a4
.. versionchanged:: 1.30.0 the maximum UV_THREADPOOL_SIZE allowed was increased from 128 to 1024.
Packit Service 7c31a4
Packit Service 7c31a4
The threadpool is global and shared across all event loops. When a particular
Packit Service 7c31a4
function makes use of the threadpool (i.e. when using :c:func:`uv_queue_work`)
Packit Service 7c31a4
libuv preallocates and initializes the maximum number of threads allowed by
Packit Service 7c31a4
``UV_THREADPOOL_SIZE``. This causes a relatively minor memory overhead
Packit Service 7c31a4
(~1MB for 128 threads) but increases the performance of threading at runtime.
Packit Service 7c31a4
Packit Service 7c31a4
.. note::
Packit Service 7c31a4
    Note that even though a global thread pool which is shared across all events
Packit Service 7c31a4
    loops is used, the functions are not thread safe.
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
Data types
Packit Service 7c31a4
----------
Packit Service 7c31a4
Packit Service 7c31a4
.. c:type:: uv_work_t
Packit Service 7c31a4
Packit Service 7c31a4
    Work request type.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:type:: void (*uv_work_cb)(uv_work_t* req)
Packit Service 7c31a4
Packit Service 7c31a4
    Callback passed to :c:func:`uv_queue_work` which will be run on the thread
Packit Service 7c31a4
    pool.
Packit Service 7c31a4
Packit Service 7c31a4
.. c:type:: void (*uv_after_work_cb)(uv_work_t* req, int status)
Packit Service 7c31a4
Packit Service 7c31a4
    Callback passed to :c:func:`uv_queue_work` which will be called on the loop
Packit Service 7c31a4
    thread after the work on the threadpool has been completed. If the work
Packit Service 7c31a4
    was cancelled using :c:func:`uv_cancel` `status` will be ``UV_ECANCELED``.
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
Public members
Packit Service 7c31a4
^^^^^^^^^^^^^^
Packit Service 7c31a4
Packit Service 7c31a4
.. c:member:: uv_loop_t* uv_work_t.loop
Packit Service 7c31a4
Packit Service 7c31a4
    Loop that started this request and where completion will be reported.
Packit Service 7c31a4
    Readonly.
Packit Service 7c31a4
Packit Service 7c31a4
.. seealso:: The :c:type:`uv_req_t` members also apply.
Packit Service 7c31a4
Packit Service 7c31a4
Packit Service 7c31a4
API
Packit Service 7c31a4
---
Packit Service 7c31a4
Packit Service 7c31a4
.. c:function:: int uv_queue_work(uv_loop_t* loop, uv_work_t* req, uv_work_cb work_cb, uv_after_work_cb after_work_cb)
Packit Service 7c31a4
Packit Service 7c31a4
    Initializes a work request which will run the given `work_cb` in a thread
Packit Service 7c31a4
    from the threadpool. Once `work_cb` is completed, `after_work_cb` will be
Packit Service 7c31a4
    called on the loop thread.
Packit Service 7c31a4
Packit Service 7c31a4
    This request can be cancelled with :c:func:`uv_cancel`.
Packit Service 7c31a4
Packit Service 7c31a4
.. seealso:: The :c:type:`uv_req_t` API functions also apply.