Skip to content

Instantly share code, notes, and snippets.

@ucotta
Created November 10, 2016 10:16
Show Gist options
  • Save ucotta/dab6fa3f5cc86d4d48ae461069d662f2 to your computer and use it in GitHub Desktop.
Save ucotta/dab6fa3f5cc86d4d48ae461069d662f2 to your computer and use it in GitHub Desktop.
Swift: NSTask with 0% cpu
// This task will run for ever a task get 0% of cpu
// dont use waitUntil because it will consume you cpu
import Foundation
extension Task {
public func waitUntilWithSleep() {
while running {
// sleep for 0.1 seconds
usleep(10000)
}
}
}
@ucotta
Copy link
Author

ucotta commented Nov 10, 2016

I created this method to run a Perfect.org server for ever...

import Foundation

let task = Task()
task.launchPath = "/path/to/my/app/web/server"
while true {
  task.launch()
  task.waitUntilWithSleep()
  print("server restarted")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment