我希望就我正在处理的部分添加弹性来获得一些意见,该过程涉及异步地与多个服务器创建多个TcpClient
连接,一旦成功建立TCP连接,然后存储{{1 }}归类(仅摘自要求)
这是我到目前为止所拥有的:
TcpClient
问题:
万一发生超时,在internal class Program
{
private static readonly TcpClient _client = new TcpClient(AddressFamily.InterNetwork);
private static SslStream _stream;
private static async Task Main(string[] args)
{
var timeoutTask = Task.Delay(5000);
var connectTask = _client.ConnectAsync("10.14.16.24", 56564);
var completedTask = await Task.WhenAny(timeoutTask, connectTask);
if (completedTask == timeoutTask)
_client.Dispose();
try
{
await connectTask;
_stream = new SslStream(_client.GetStream(), false);
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
}
}
}
方法返回connectTask
或WaitingForActivation
之前,ConnectAsync
的状态仍为“ Faulted
”。知道RanToCompletion
不支持ConnectAsync
欢迎提出任何建议或改进。 谢谢
答案 0 :(得分:1)
作为@Kelly Elton 答案的扩展,您似乎应该抓住
NullReferenceException
ObjectDisposedException
SocketException
所以也许正确的 catch 语句是
#if NET5_0 //_OR_GREATER?
catch (SocketException ex) when (ct.IsCancellationRequested)
#elif NETCOREAPP2_0_OR_GREATER
catch (ObjectDisposedException ex) when (ct.IsCancellationRequested)
#elif NETFRAMEWORK && NET40_OR_GREATER
catch (NullReferenceException ex) when (ct.IsCancellationRequested)
#else
#error Untested target framework, use with care!
catch (???) when (ct.IsCancellationRequested)
#endif
答案 1 :(得分:0)
根据您使用的from selenium import webdriver
def func1(driver):
driver.get('https://example.com')
def func2(driver):
return driver.title
if __name__ == '__main__':
driver = webdriver.Chrome()
func1(driver)
title = func2(driver)
print(title)
driver.quit()
,您的里程可能会有所不同
.net