File size: 2,091 Bytes
851e5ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Helper functions for properly running synchronous operations in async context.
"""
import asyncio
from functools import wraps
from typing import Callable, TypeVar, Any
import logging

logger = logging.getLogger(__name__)

T = TypeVar('T')

async def run_in_thread(func: Callable[..., T], *args: Any, **kwargs: Any) -> T:
    """
    Run a synchronous function in a thread pool to avoid blocking the event loop.

    This should be used for any I/O-bound or CPU-bound sync operations like:
    - Video processing
    - File operations
    - YouTube downloads
    - Transcript generation
    """
    from ..media_storage import _workspace

    try:
        if _workspace.get() is None:
            return await asyncio.to_thread(func, *args, **kwargs)
        operation = asyncio.create_task(asyncio.to_thread(func, *args, **kwargs))
        try:
            return await asyncio.shield(operation)
        except asyncio.CancelledError:
            # Python cannot stop a running thread. Keep its media leases until
            # it has stopped reading/writing, then propagate cancellation.
            while not operation.done():
                try:
                    await asyncio.shield(operation)
                except asyncio.CancelledError:
                    continue
                except BaseException:
                    break
            if not operation.cancelled():
                operation.exception()
            raise
    except Exception as e:
        logger.error(f"Error running {func.__name__} in thread: {e}")
        raise


def async_wrap(func: Callable[..., T]) -> Callable[..., Any]:
    """
    Decorator to automatically wrap synchronous functions to run in thread pool.

    Usage:
        @async_wrap
        def my_sync_function(arg1, arg2):
            # ... blocking operations ...
            return result

        # Can now be awaited:
        result = await my_sync_function(arg1, arg2)
    """
    @wraps(func)
    async def wrapper(*args: Any, **kwargs: Any) -> T:
        return await run_in_thread(func, *args, **kwargs)

    return wrapper