而不是繼續註釋,這就是我想你可能是試圖做:
從朱莉婭REPL:
julia> server = listen(2000)
Base.TCPServer(active)
julia> @async begin
while true
sock = accept(server)
print(readstring(sock))
end
end
從另一個終端:
~ $ nc localhost 2000
Hello from the other terminal
[Ctrl-D] % i.e. signal end of file. this closes the connection
在朱利亞報告中,當您發送EOF信號時,您會看到「從其他終端發出的問候」打印,但否則朱莉婭的提示將照常繼續。如果從netcat終端重複此過程,則會再次在REPL中顯示消息,因爲套接字會在while循環內繼續重新激活。
理想情況下,如果你想關閉整個事情,你會先close(sock)
,然後close(server)
。但是,您不能直接關閉套接字,因爲它處於「while」循環中,並且它一直處於重新激活狀態,並且您無法直接訪問變量「sock」。
因此,您只能關閉服務器,完全期待錯誤。 所以趕上一個try塊,而不是
編輯:對不起,是我不好,除了涉及到插座,而不是服務器,因此你需要用是在try catch塊的異步塊內,而不是:
@async begin
while true
try
sock = accept(server)
print(readstring(sock))
catch ex
print("exiting while loop")
break
end
end
end
我不能重現這一點。做一個正常的(不是'@ async')例子,'close(sock)'和'close(server)'都可以正常工作(julia 0.6.0)。上面的示例中,我無法訪問服務器和sock以關閉它們 –
也許您打算在「@ async」任務之外定義服務器和套接字*? –
只有在嘗試關閉服務器而未先關閉套接字時,我纔會收到您描述的錯誤。 (如果你在異步模塊外部使用'server = listen(2000)',那麼在REPL中可能會出現這種情況,而且由於你打印的是你自己的字符串,而不是從流中讀取,所以流不會關閉。一個更好的例子? –