Tuesday, June 8, 2021

Hello swift async and await for Swift 5.5

(1) This is to test the new async and await feature for Swift 5.5 dev on Linux. First Download and install packages in Ubuntu 20.04.
Shell script for setup Swift 5.5   Select all
sudo apt-get install -y binutils git gnupg2 libc6-dev libcurl4 libedit2 libgcc-9-dev libpython2.7 libsqlite3-0 libstdc++-9-dev libxml2 libz3-dev pkg-config tzdata zlib1g-dev cd ${HOME} wget https://swift.org/builds/swift-5.5-branch/ubuntu2004/swift-5.5-DEVELOPMENT-SNAPSHOT-2021-06-02-a/swift-5.5-DEVELOPMENT-SNAPSHOT-2021-06-02-a-ubuntu20.04.tar.gz tar xzvf swift-5.5-DEVELOPMENT-SNAPSHOT-2021-06-02-a-ubuntu20.04.tar.gz export PATH=${HOME}/swift-5.5-DEVELOPMENT-SNAPSHOT-2021-06-02-a-ubuntu20.04/usr/bin:$PATH


(2) Create swift package for executable
Shell script testing async and await   Select all
cd ${HOME} mkdir -p ${HOME}/AsyncSwift cd ${HOME}/AsyncSwift swift package init --type executable


(3) Amend ${HOME}/AsyncSwift/Package.swift to have swift setting flags
Package.swift   Select all
// swift-tools-version:5.5 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription let package = Package( name: "AsyncSwift", products: [ .executable(name: "AsyncSwift", targets: ["AsyncSwift"]) ], dependencies: [ // none for now.. ], targets: [ .executableTarget(name: "AsyncSwift", swiftSettings: [ .unsafeFlags([ "-parse-as-library", "-Xfrontend", "-disable-availability-checking", "-Xfrontend", "-enable-experimental-concurrency", ]) ] ), .testTarget(name: "AsyncSwiftTests", dependencies: ["AsyncSwift"]), ] )


(4) Edit ${HOME}/AsyncSwift/Sources/AsyncSwift/main.swift
Sources/AsyncSwift/main.swift   Select all
import Foundation func calculateFirstNumber() async -> Int { print("First number is now being calculated...") return await withUnsafeContinuation { c in DispatchQueue.main.asyncAfter(deadline: .now() + 2) { print("First number is now ready.") c.resume(returning: 42) } } } func calculateSecondNumber() async -> Int { print("Second number is now being calculated...") return await withUnsafeContinuation { c in DispatchQueue.main.asyncAfter(deadline: .now() + 1) { print("Second number is now ready.") c.resume(returning: 6) } } } func calculateThirdNumber() async -> Int { print("Third number is now being calculated...") return await withUnsafeContinuation { c in DispatchQueue.main.asyncAfter(deadline: .now() + 3) { print("Third number is now ready.") c.resume(returning: 69) } } } func trace(task: Int) async { // Generates a random integer in the [0, task] range print("Task \(task) started") sleep( UInt32.random(in: 0...UInt32(task)) ) print("Task \(task) completed") } @main struct MyProgram { static func main() async { print("Hello, swift async!\n") print("\nSerial queue, asynchronous execution\n") for i in 5...10 { print("Submitting task \(i)") async { await trace(task: i) } } async let x = calculateFirstNumber() let y = await calculateSecondNumber() let z = await calculateThirdNumber() await print(x + y + z) sleep(2) await print("Program ended") } } /* [6/6] Build complete! Hello, swift async! First number is now being calculated... First number is now ready. Second number is now being calculated... Second number is now ready. Third number is now being calculated... Third number is now ready. 117 */


(5) build and run executable
Shell script for building and running   Select all
cd ${HOME}/AsyncSwift swift package clean swift build swift run


No comments: