简体   繁体   English

在继续下一个任务之前等待任务完成

[英]Waiting for a task to finish before moving on to next one

I'm using NMSSH in order to connect to a server and then grab data off that server. 我正在使用NMSSH连接到服务器,然后从该服务器上获取数据。 However, connecting to the server, performing commands, and retrieving the data all take time to complete. 但是,连接到服务器,执行命令和检索数据都需要一些时间来完成。 Previously, I was using sleep() commands in order to allow the program to pause and let those commands finish running but if I don't specify the exact amount of time to sleep the data may not fully finish downloading (since the data varies in size its hard to determine the correct amount of sleep). 以前,我使用sleep()命令以允许程序暂停并让这些命令完成运行但如果我没有指定准确的睡眠时间,则数据可能无法完全完成下载(因为数据在大小很难确定正确的睡眠量)。 So after doing some research it seemed that Dispatch Groups and Async operations were the correct way to go. 因此,在进行一些研究后,似乎Dispatch Groups和Async操作是正确的方法。 I implemented these as follows: 我实现了如下:

let queue = DispatchQueue(label: "myqueue")
let group = DispatchGroup()    
let myfirstconnection: SSH = SSH()
myfirstconnection.hostname = "@hostname"
myfirstconnection.username = "user"
myfirstconnection.password = "password"

queue.async(group: group) {
    myfirstconnection.connectToServer()
    print("1")
}
group.wait()

queue.async(group: group) {
    myfirstconnection.performCommand()
    print("2")
}
group.wait()

queue.async(group: group) {
    myfirstconnection.retrieveData()
    print("3")
}
group.wait()

queue.async(group: group) {
    myfirstconnection.endSession()
    print("4")
}
group.wait()

But, this does not seem to be working correctly because all the commands still run at the same time. 但是,这似乎没有正常工作,因为所有命令仍然在同一时间运行。 Essentially I need each block above to run, then wait until completed before moving to the next block. 基本上我需要上面的每个块运行,然后等到完成后再移动到下一个块。

The NMSSH framework follows the (very!) common delegate pattern (eg, the UIText​Field​ view and its UIText​Field​Delegate protocol from UIKit framework; etc). 所述NMSSH框架遵循(非常!)共同委托模式 (例如, UIText​Field​视图及其UIText​Field​Delegate从协议UIKit框架;等)。 As such, all network operations run in the background and, upon completion, fire a corresponding protocol methods. 因此,所有网络操作都在后台运行,并在完成后启动相应的协议方法。

You should implement the NMSSHSessionDelegate and NMSSHChannelDelegate protocols to be notified of such events. 您应该实现NMSSHSessionDelegateNMSSHChannelDelegate协议以通知此类事件。 Keep in mind that your code structure will look very different when doing so. 请记住,这样做时代码结构会有很大差异。

Hope that helps ;-) 希望有所帮助;-)

Ok , first of all using sleep() won't work at all, because how you want to estimate the time it takes to download something , some connections might be fast some might be slow. 好吧,首先使用sleep()根本不起作用,因为你想要估计下载内容的时间,某些连接可能很快,有些可能会很慢。 And from my understanding of your problem you want to execute some task and then do something with the data and then continue to the next task until they are done.One item at a time. 从我对你的问题的理解,你想要执行一些任务,然后对数据做一些事情,然后继续下一个任务,直到它们完成。一次一个项目。 For this situation I have a solution but I'm not sure if that's the best way to do it or not but it works. 对于这种情况,我有一个解决方案,但我不确定这是否是最好的方式,但它的工作原理。

        let serialQueue = DispatchQueue(label: "serialQueueWork" )
        let semaphore = DispatchSemaphore(value: 1)

        let task1 = DispatchWorkItem {
            semaphore.wait()
            print("1 started.")
            for _ in 0...5{
                print("♥️")
            }
        }

        let task2 = DispatchWorkItem  {
            semaphore.wait()
            print("2 started.")
            for _ in 0...5{
                print("♣️")
            }
        }

        task1.notify(queue: DispatchQueue.main) {
            print("1 is finished.")
            semaphore.signal()
        }

        task2.notify(queue: DispatchQueue.main) {
            print("2 is finished")
            semaphore.signal()
        }

        serialQueue.async(execute: task1 )
        serialQueue.async(execute: task2 )


// console result

1 started. 1开始了。 ♥️ ♥️ ♥️ ♥️ ♥️ ♥️ 1 is finished. ♥️♥️♥♥♥♥️♥️1完成了。 2 started. 2开始了。 ♣️ ♣️ ♣️ ♣️ ♣️ ♣️ 2 is finished ♣️♣️♣️♣️♣️♣️2完成

Using semaphore you take control of when the queue can continue to work. 使用semaphore可以控制队列何时可以继续工作。 using DispatchWorkItem you get notified When the work is done and let the queue continue. 使用DispatchWorkItem会收到通知当工作完成并让队列继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM