|
43 | 43 |
|
44 | 44 | from twisted.internet import defer
|
45 | 45 | from twisted.internet.interfaces import IDelayedCall
|
| 46 | +from twisted.python.threadpool import ThreadPool |
46 | 47 | from twisted.web.resource import Resource
|
47 | 48 |
|
48 | 49 | from synapse.api import errors
|
|
79 | 80 | from synapse.http.site import SynapseRequest
|
80 | 81 | from synapse.logging.context import (
|
81 | 82 | defer_to_thread,
|
| 83 | + defer_to_threadpool, |
82 | 84 | make_deferred_yieldable,
|
83 | 85 | run_in_background,
|
84 | 86 | )
|
@@ -1733,6 +1735,33 @@ async def defer_to_thread(
|
1733 | 1735 | """
|
1734 | 1736 | return await defer_to_thread(self._hs.get_reactor(), f, *args, **kwargs)
|
1735 | 1737 |
|
| 1738 | + async def defer_to_threadpool( |
| 1739 | + self, |
| 1740 | + threadpool: ThreadPool, |
| 1741 | + f: Callable[P, T], |
| 1742 | + *args: P.args, |
| 1743 | + **kwargs: P.kwargs, |
| 1744 | + ) -> T: |
| 1745 | + """Runs the given function in a separate thread from the given thread pool. |
| 1746 | +
|
| 1747 | + Allows specifying a custom thread pool instead of using the default Synapse |
| 1748 | + one. To use the default Synapse threadpool, use `defer_to_thread` instead. |
| 1749 | +
|
| 1750 | + Added in Synapse v1.140.0. |
| 1751 | +
|
| 1752 | + Args: |
| 1753 | + threadpool: The thread pool to use. |
| 1754 | + f: The function to run. |
| 1755 | + args: The function's arguments. |
| 1756 | + kwargs: The function's keyword arguments. |
| 1757 | +
|
| 1758 | + Returns: |
| 1759 | + The return value of the function once ran in a thread. |
| 1760 | + """ |
| 1761 | + return await defer_to_threadpool( |
| 1762 | + self._hs.get_reactor(), threadpool, f, *args, **kwargs |
| 1763 | + ) |
| 1764 | + |
1736 | 1765 | async def check_username(self, username: str) -> None:
|
1737 | 1766 | """Checks if the provided username uses the grammar defined in the Matrix
|
1738 | 1767 | specification, and is already being used by an existing user.
|
|
0 commit comments