|
// main.wren (Corrected)
|
|
import "requests" for Requests
|
|
|
|
// This class provides a hook back into the C host
|
|
foreign class Host {
|
|
foreign static signalDone()
|
|
}
|
|
|
|
var mainFiber = Fiber.new {
|
|
System.print("--- Running 20 CONCURRENT GET requests ---")
|
|
|
|
var completed = 0
|
|
|
|
for (i in 1..1000) {
|
|
Requests.get("https://jsonplaceholder.typicode.com/posts/%(i)", null) { |err, res|
|
|
if (err) {
|
|
System.print("Request #%(i) [GET] Error: %(err)")
|
|
} else {
|
|
System.print("Request #%(i) [GET] Status: %(res.statusCode)")
|
|
System.print("Request #%(i) [GET] Status: %(res.body)")
|
|
}
|
|
// CORRECTED: Create a new fiber for each atomic operation
|
|
Fiber.new { completed = completed + 1 }.call()
|
|
}
|
|
}
|
|
|
|
// Wait for GET requests to finish
|
|
while (completed < 1000) {
|
|
Fiber.yield()
|
|
}
|
|
|
|
System.print("\n--- Running 20 CONCURRENT POST requests ---")
|
|
|
|
var postBody = """
|
|
{ "title": "wren-test" }
|
|
"""
|
|
var headers = { "Content-Type": "application/json; charset=UTF-8" }
|
|
var postCompleted = 0
|
|
for (i in 1..1000) {
|
|
Requests.post("https://jsonplaceholder.typicode.com/posts", postBody, headers) { |err, res|
|
|
if (err) {
|
|
System.print("Request #%(i) [POST] Error: %(err)")
|
|
} else {
|
|
System.print("Request #%(i) [POST] Response Code: %(res.statusCode)")
|
|
}
|
|
// CORRECTED: Create a new fiber for each atomic operation
|
|
Fiber.new { postCompleted = postCompleted + 1 }.call()
|
|
}
|
|
}
|
|
|
|
// Wait for POST requests to finish
|
|
while (postCompleted < 1000) {
|
|
Fiber.yield()
|
|
}
|
|
|
|
System.print("\n--- All concurrent requests finished. ---")
|
|
|
|
// Tell the C host that we are done.
|
|
Host.signalDone()
|
|
}
|