Does Concurrency Work in Docker?

my docker file is

# Use the official Swift image from the Docker Hub
FROM swift:5.10

# Set a working directory inside the container
WORKDIR /app

# Copy the Package.swift and Sources directory into the container at /app
COPY Package.swift ./
COPY Sources ./Sources

# Build the Swift package
RUN swift build

# Run the executable produced by the build
CMD ["swift", "run", "SomeFile"]

and my code contains

import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
#if canImport(_Concurrency)
import _Concurrency
#endif

....

let (data, _) = try await URLSession.shared.data(for: request)


but got

2.409 /app/Sources/MyFile.swift:34:57: error: value of type 'URLSession' has no member 'data'
2.409 let (data, _) = try await URLSession.shared.data(for: request)
2.409 ~~~~~~~~~~~~~~~~~ ^~~~

do you know how to fix it?

Corelibs foundation is a different implementation on Linux currently, and it has many missing APIs. So what you’re experiencing is a different library on Linux not having the api you expect.

There’s an active process to resolve this weird situation by implementing foundation in swift which is over here: GitHub - apple/swift-foundation: The Foundation project and then both platforms will be using the exact same implementation, resolving the issue you’ve hit.

In practice for http clients though I’d recommend using the async http client library anyway: GitHub - swift-server/async-http-client: HTTP client library built on SwiftNIO as it’s more geared towards the server ecosystem.

Hope this helps

2 Likes

thanks a lot it does help!

1 Like

Unless you happen to use something of that stable and solid ā€ždeprecatedā€œ API thatā€˜s not going to be implemented in the new one

Actually, just today Jonathan submitted a PR Add async URLSession methods by jrflat Ā· Pull Request #4970 Ā· apple/swift-corelibs-foundation Ā· GitHub implementing the missing methods in the corelibs URLSession :slight_smile:

So while the medium and long term story remains about GitHub - apple/swift-foundation: The Foundation project this specific issue would also be fixed in corelibs :slight_smile:

7 Likes

Thatā€˜s very good news. There are so many important pull requests lingering, a bit of traction would suit that project very well.

1 Like