pythonx-concurrent
Structured concurrency for Python on Kotlin Multiplatform: coroutines that interoperate with Kotlin coroutines and asyncio.Kotlin Multiplatform 위 Python의 구조적 동시성: Kotlin 코루틴, asyncio와 함께 도는 코루틴.
Overview개요
pythonx-concurrent gives Python apps on Kotlin Multiplatform one concurrency model, with Kotlin's vocabulary in Python's spelling, that bridges asyncio and Kotlin coroutines in both directions and runs work on the right thread of every platform.
pythonx-concurrent는 Kotlin Multiplatform 위의 Python 앱에 하나의 동시성 모델을 줍니다. Kotlin의 어휘를 Python 표기로 쓰고, asyncio와 Kotlin 코루틴을 양방향으로 잇고, 작업을 모든 플랫폼의 알맞은 스레드에서 돌립니다.
The problem it solves푸는 문제
On Android, work runs in Kotlin coroutines; in Python, in asyncio. An app written in Python on Kotlin Multiplatform lives in both: it awaits Kotlin suspend functions, collects Kotlin flows, must not block the UI thread, and should stop work when a screen goes away. Without a common model, each of these is solved by hand and cancellation is lost at the boundary.
Android에서 작업은 Kotlin 코루틴으로, Python에서는 asyncio로 돕니다. Kotlin Multiplatform 위에서 Python으로 쓴 앱은 둘 다에 삽니다. Kotlin suspend 함수를 await하고, Kotlin flow를 수집하고, UI 스레드를 막으면 안 되고, 화면이 사라지면 작업을 멈춰야 합니다. 공통 모델이 없으면 이걸 하나씩 손으로 풀고, 취소는 경계에서 사라집니다.
How it works동작 방식
from pythonx.concurrent import Scope, Dispatchers, with_context
async def load(user_id):
async with Scope() as s: # coroutineScope { }
profile = s.defer(fetch_profile, user_id) # async { }
s.launch(log_visit, user_id) # launch { }
data = await with_context(Dispatchers.IO, read_cache, user_id)
return profile.result(), data
- Scopes and jobs.
Scope,launch,defer, supervision, timeouts; no task outlives its scope. - Dispatchers.
Mainon the UI thread,IOfor blocking calls,Defaultfor CPU work, and any Kotlin dispatcher. - Bridging. Python awaits Kotlin suspend functions and flows; Kotlin awaits Python coroutines; cancellation and exceptions cross both ways.
from pythonx.concurrent import Scope, Dispatchers, with_context
async def load(user_id):
async with Scope() as s: # coroutineScope { }
profile = s.defer(fetch_profile, user_id) # async { }
s.launch(log_visit, user_id) # launch { }
data = await with_context(Dispatchers.IO, read_cache, user_id)
return profile.result(), data
- 스코프와 job.
Scope,launch,defer, 감독, 타임아웃. 어떤 태스크도 스코프보다 오래 살지 않습니다. - 디스패처. UI 스레드의
Main, 막히는 호출용IO, CPU 작업용Default, 그리고 모든 Kotlin 디스패처. - 잇기. Python이 Kotlin suspend 함수와 flow를 await하고, Kotlin이 Python 코루틴을 await하며, 취소와 예외가 양방향으로 건넙니다.
Design decisions설계 결정
- Built on asyncio, not beside it. Standard library code keeps working inside tasks.
- Structured only. No global scope.
- Python on Kotlin's threads ("Kotlin mode"). A Python coroutine is stepped by a Kotlin coroutine, one
send()at a time. Between steps it holds no GIL and no native frame, so it can run on JVM virtual threads and move between threads. Blocking-style Python is not put on virtual threads, because a native frame pins them. - Parallel through free-threaded CPython, not sub-interpreters.
- asyncio 옆이 아니라 위에. 표준 라이브러리 코드가 태스크 안에서 그대로 돕니다.
- 구조적으로만. 전역 스코프는 없습니다.
- Kotlin 스레드 위의 Python("Kotlin 모드"). Kotlin 코루틴이 Python 코루틴을
send()한 번씩 실행합니다. 단계 사이에는 GIL도 네이티브 프레임도 쥐지 않아서 JVM 가상 스레드에서 돌고 스레드를 옮길 수 있습니다. 막히는 방식의 Python은 네이티브 프레임이 가상 스레드를 묶어 버려서 가상 스레드에 올리지 않습니다. - 병렬성은 free-threaded CPython에서, 서브 인터프리터가 아닙니다.
Where it fits생태계 안의 위치
It needs binder features listed in python-multiplatform (coroutine stepping, flows, cancellation mapping, GIL release while Kotlin runs). pythonx-compose builds its UI coroutine scopes on it; pythonx-graphics loads assets and runs agent loops with it.
python-multiplatform에 정리된 바인더 기능(코루틴 단계 실행, flow, 취소 매핑, Kotlin 실행 중 GIL 해제)이 필요합니다. pythonx-compose가 UI 코루틴 스코프를 이 위에 만들고, pythonx-graphics가 에셋 로딩과 에이전트 루프에 씁니다.
Where it stands현재 상태
Specified in detail, name reserved on PyPI. Implementation in the pythonx turn.
스펙을 자세히 썼고 PyPI 이름을 선점했습니다. 구현은 pythonx 차례에 합니다.
Future design미래 설계
- Goroutine-style scheduling of many tasks over few carrier threads, parallel on 3.15t.
- Work stealing of started tasks in Kotlin mode, which asyncio tasks cannot do.
- Real threads in the browser under cross-origin isolation.
- 적은 캐리어 스레드 위에 많은 태스크를 얹는 고루틴 방식 스케줄링, 3.15t에서 병렬.
- Kotlin 모드에서 이미 시작한 태스크의 작업 훔치기. asyncio 태스크는 할 수 없는 일입니다.
- 교차 출처 격리 아래 브라우저의 진짜 스레드.
Roadmap로드맵
After pythonx-compose's beta, in the order of the ecosystem roadmap.
pythonx-compose 베타 뒤, 생태계 로드맵 순서대로.