Skip to content

Async/Await support - #72

Open
rickmark wants to merge 1 commit into
pvieito:mainfrom
rickmark:main
Open

Async/Await support#72
rickmark wants to merge 1 commit into
pvieito:mainfrom
rickmark:main

Conversation

@rickmark

Copy link
Copy Markdown

This adds support for calling async/await functions in python.

Copilot AI lite review requested due to automatic review settings September 13, 2026 04:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved GIL-safety, compatibility, and event-loop issues block approval.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Adds Swift async/await support for invoking Python callables and awaitables.

Changes:

  • Adds async dynamic-call overloads and awaitable resolution.
  • Adds Python GIL symbol bindings.
  • Adds async tests for calls, callbacks, errors, and awaiting.
File summaries
File Changes
Tests/PythonKitTests/PythonAsyncTests.swift Adds async behavior tests.
PythonKit/PythonLibrary+Symbols.swift Adds GIL management symbols.
PythonKit/Python.swift Implements async calls and awaitable handling.
Review details

Suppressed comments (1)

PythonKit/Python.swift:307

  • Although these overloads are async, runner.run executes the entire Python coroutine synchronously before returning, so waits such as await asyncio.sleep(...) block the Swift concurrency executor thread. Concurrent calls can starve the cooperative pool; run the Python event loop on a dedicated thread/queue and bridge completion back to Swift, or provide a clearly blocking API instead.
            let runner = try runnerClass.throwing.callSynchronously(
                withArguments: [] as [PythonConvertible])
            defer {
                _ = try? runner.close.throwing.callSynchronously(
                    withArguments: [])
            }
            return try runner.run.throwing.callSynchronously(
                withArguments: [result])
  • Files reviewed: 3/3 changed files
  • Comments generated: 5
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread PythonKit/Python.swift
Comment on lines +286 to +288
let gstate = PyGILState_Ensure()
defer {
PyGILState_Release(gstate)
Comment thread PythonKit/Python.swift
PyGILState_Release(gstate)
}

let inspect = Python.import("inspect")
Comment thread PythonKit/Python.swift
Comment on lines +312 to +318
_ = try? asyncio.set_event_loop.throwing.callSynchronously(
withArguments: [eventLoop])
defer {
_ = try? eventLoop.close.throwing.callSynchronously(
withArguments: [])
_ = try? asyncio.set_event_loop.throwing.callSynchronously(
withArguments: [Python.None])
Comment thread PythonKit/Python.swift
Comment on lines +714 to +716
/// Resolves `self` if it is a Python awaitable object (such as a coroutine,
/// Task, or Future), returning the completed result. If `self` is not
/// awaitable, returns `self`.
private var canUseAsyncPython: Bool {
let versionMajor = Python.versionInfo.major
let versionMinor = Python.versionInfo.minor
return (versionMajor == 3 && versionMinor >= 13) || versionMajor > 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants