PHP 8.5.2
Preview: threads.py Size: 4.13 KB
/lib/python3/dist-packages/twisted/internet/threads.py

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Extended thread dispatching support.

For basic support see reactor threading API docs.
"""

from __future__ import annotations

import queue as Queue
from typing import Callable, TypeVar

from typing import ParamSpec

from twisted.internet import defer
from twisted.internet.interfaces import IReactorFromThreads
from twisted.python import failure
from twisted.python.threadpool import ThreadPool

_P = ParamSpec("_P")
_R = TypeVar("_R")


def deferToThreadPool(
    reactor: IReactorFromThreads,
    threadpool: ThreadPool,
    f: Callable[_P, _R],
    *args: _P.args,
    **kwargs: _P.kwargs,
) -> defer.Deferred[_R]:
    """
    Call the function C{f} using a thread from the given threadpool and return
    the result as a Deferred.

    This function is only used by client code which is maintaining its own
    threadpool.  To run a function in the reactor's threadpool, use
    C{deferToThread}.

    @param reactor: The reactor in whose main thread the Deferred will be
        invoked.

    @param threadpool: An object which supports the C{callInThreadWithCallback}
        method of C{twisted.python.threadpool.ThreadPool}.

    @param f: The function to call.
    @param args: positional arguments to pass to f.
    @param kwargs: keyword arguments to pass to f.

    @return: A Deferred which fires a callback with the result of f, or an
        errback with a L{twisted.python.failure.Failure} if f throws an
        exception.
    """
    d: defer.Deferred[_R] = defer.Deferred()

    def onResult(success: bool, result: _R | BaseException) -> None:
        if success:
            reactor.callFromThread(d.callback, result)
        else:
            reactor.callFromThread(d.errback, result)

    threadpool.callInThreadWithCallback(onResult, f, *args, **kwargs)

    return d


def deferToThread(f, *args, **kwargs):
    """
    Run a function in a thread and return the result as a Deferred.

    @param f: The function to call.
    @param args: positional arguments to pass to f.
    @param kwargs: keyword arguments to pass to f.

    @return: A Deferred which fires a callback with the result of f,
    or an errback with a L{twisted.python.failure.Failure} if f throws
    an exception.
    """
    from twisted.internet import reactor

    return deferToThreadPool(reactor, reactor.getThreadPool(), f, *args, **kwargs)


def _runMultiple(tupleList):
    """
    Run a list of functions.
    """
    for f, args, kwargs in tupleList:
        f(*args, **kwargs)


def callMultipleInThread(tupleList):
    """
    Run a list of functions in the same thread.

    tupleList should be a list of (function, argsList, kwargsDict) tuples.
    """
    from twisted.internet import reactor

    reactor.callInThread(_runMultiple, tupleList)


def blockingCallFromThread(reactor, f, *a, **kw):
    """
    Run a function in the reactor from a thread, and wait for the result
    synchronously.  If the function returns a L{Deferred}, wait for its
    result and return that.

    @param reactor: The L{IReactorThreads} provider which will be used to
        schedule the function call.
    @param f: the callable to run in the reactor thread
    @type f: any callable.
    @param a: the arguments to pass to C{f}.
    @param kw: the keyword arguments to pass to C{f}.

    @return: the result of the L{Deferred} returned by C{f}, or the result
        of C{f} if it returns anything other than a L{Deferred}.

    @raise Exception: If C{f} raises a synchronous exception,
        C{blockingCallFromThread} will raise that exception.  If C{f}
        returns a L{Deferred} which fires with a L{Failure},
        C{blockingCallFromThread} will raise that failure's exception (see
        L{Failure.raiseException}).
    """
    queue = Queue.Queue()

    def _callFromThread():
        result = defer.maybeDeferred(f, *a, **kw)
        result.addBoth(queue.put)

    reactor.callFromThread(_callFromThread)
    result = queue.get()
    if isinstance(result, failure.Failure):
        result.raiseException()
    return result


__all__ = [
    "deferToThread",
    "deferToThreadPool",
    "callMultipleInThread",
    "blockingCallFromThread",
]

Directory Contents

Dirs: 3 × Files: 57

Name Size Perms Modified Actions
- drwxr-xr-x 2026-01-08 12:56:22
Edit Download
test DIR
- drwxr-xr-x 2026-01-08 12:56:22
Edit Download
- drwxr-xr-x 2026-01-08 12:56:23
Edit Download
18.84 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.14 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
10.87 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
46.82 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
22.75 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.85 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
95.88 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
76.59 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.73 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
13.14 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.16 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.42 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.24 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.55 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
512 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
14.06 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
95.89 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
10.56 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1006 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.83 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
20.88 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
43.14 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
26.93 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
853 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.77 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.96 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.20 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.47 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1006 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
32.90 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
53.97 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
29.12 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
4.13 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.92 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
18.18 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
21.98 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.48 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
14.84 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.15 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.27 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.96 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
743 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
12.29 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
12.61 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.39 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.97 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
8.49 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.99 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
4.88 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.82 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
9.46 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
14.06 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
70.68 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
11.31 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
4.69 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.04 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
521 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).