Async/Await support - #72
Open
rickmark wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
🟡 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.runexecutes the entire Python coroutine synchronously before returning, so waits such asawait 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 on lines
+286
to
+288
| let gstate = PyGILState_Ensure() | ||
| defer { | ||
| PyGILState_Release(gstate) |
| PyGILState_Release(gstate) | ||
| } | ||
|
|
||
| let inspect = Python.import("inspect") |
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 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds support for calling async/await functions in python.