TNonblockingServerTransport 未使用 Thrift C# 库实现
TNonblockingServerTransport not implemented with Thrift C# library
使用 Apache Thrift [https://github.com/apache/thrift] 在 C# 中创建非阻塞服务器时,无法识别以下 Classes/Types:
TNonblockingServerTransport
TNonblockingServer
我想从我的 win10 笔记本电脑发送命令来控制在高性能服务器 (ubuntu) 上执行的耗时计算。这就是我来到 Apache Thrift 的原因。我找到了官方的 C# 版本教程 [https://github.com/apache/thrift/tree/master/tutorial/csharp] 并且效果很好。本教程使用所谓的阻塞模式 (TSimpleServer)。但在我的情况下,耗时的计算过程应该是可中断的。因此,我必须使用非阻塞服务器。
逻辑很简单。对于服务器,我使用了私有标志 forceStop。如果客户端调用 Stop(),forceStop 将设置为 true,计算循环将中断。
// #Server#
// Server Set-Up
private void SetUp()
{
try
{
CalculatorHandler handler = new CalculatorHandler();
Calculator.Processor processor = new
Calculator.Processor(handler);
var serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(processor, serverTransport);
// Use this for a multithreaded server
// server = new TThreadPoolServer(processor, serverTransport);
Console.WriteLine("Starting the server...");
server.Serve();
}
catch (Exception x)
{
Console.WriteLine(x.StackTrace);
}
}
private bool forceStop;
public int TimeConsumingOperation(int n1, int n2)
{
Console.WriteLine("add({0},{1})", n1, n2);
for (int i = 0; i < 10; i++)
{
//calculating
Thread.Sleep(500);
if (forceStop)
{
Quit();
}
}
return n1 + n2;
}
public void Stop()
{
forceStop = true;
}
// Client
// Button#1 Click callback
private void Button_Start_Click()
{
client.TimeConsumingOperation(0,0);
}
// Button#2 Click callback
private void Button_Stop_Click()
{
client.Stop();
}
//
我在 java [https://chamibuddhika.wordpress.com/2011/10/02/apache-thrift-quickstart-tutorial/] 中找到了一些有用的示例。我已经尽力将non-block server的java代码转换成对应的C#代码,但是我发现C#中好像没有TNonblockingServerTransport。谁能帮我解决这个问题?
// Java Code
public class NonblockingServer {
private void start() {
try {
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
ArithmeticService.Processor processor = new ArithmeticService.Processor(new ArithmeticServiceImpl());
TServer server = new TNonblockingServer(new TNonblockingServer.Args(serverTransport).
processor(processor));
System.out.println("Starting server on port 7911 ...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
NonblockingServer srv = new NonblockingServer();
srv.start();
}
}
这个问题实际上有两个答案。
可能的实现
首先,您的设置存在缺陷。
// #Server#
// Server Set-Up
private bool forceStop;
public void Stop()
{
forceStop = true;
}
假设我们有两个客户端,都开始了新的计算。现在一位客户想要中止。会发生什么?
解决方案是以这样一种方式构建它:计算是一个单独的业务逻辑对象,在 TimeConsumingOperation()
实例化并通过某种方式提供给客户端,通常是通过返回某种 ID .
当客户端现在想要中止时,它会调用 Stop(calcualtionID)
。服务器端逻辑现在将调用路由到实现并触发任何中止机制,C# 可能是 CancellationToken
.
计算完成后,需要第三次调用从服务器端查询最终结果。请注意,我们仍然使用 TSimpleServer
并且它起作用的原因是因为我们通过 API 设计避免了阻塞调用。
非阻塞服务器
是的,C# 还没有实现。由于 Thrift 是开源的,这可能仅仅意味着到目前为止 运行 没有人进入该用例并且想花时间在实现上。这并不是说这样的用例可能不存在。
存在的是
- 线程和线程池服务器
Task.Run(() => { your code })
这可能有助于解决您的用例。此外,当与 ASP.NET 一起使用时,不需要非阻塞服务器,因为运行时已经为多个连接提供了足够的支持。
底线
有一些方法可以解决您遇到的限制。另一个替代方案可能是 become a contributor by porting one of the existing (e.g. Java) nonblocking implementation to NetStd(首选,因为 C# 和 NetCore 将在下一个版本中成熟为 "deprecated" 状态,并且最终都将被 NetStd 取代)
使用 Apache Thrift [https://github.com/apache/thrift] 在 C# 中创建非阻塞服务器时,无法识别以下 Classes/Types:
TNonblockingServerTransport
TNonblockingServer
我想从我的 win10 笔记本电脑发送命令来控制在高性能服务器 (ubuntu) 上执行的耗时计算。这就是我来到 Apache Thrift 的原因。我找到了官方的 C# 版本教程 [https://github.com/apache/thrift/tree/master/tutorial/csharp] 并且效果很好。本教程使用所谓的阻塞模式 (TSimpleServer)。但在我的情况下,耗时的计算过程应该是可中断的。因此,我必须使用非阻塞服务器。
逻辑很简单。对于服务器,我使用了私有标志 forceStop。如果客户端调用 Stop(),forceStop 将设置为 true,计算循环将中断。
// #Server#
// Server Set-Up
private void SetUp()
{
try
{
CalculatorHandler handler = new CalculatorHandler();
Calculator.Processor processor = new
Calculator.Processor(handler);
var serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(processor, serverTransport);
// Use this for a multithreaded server
// server = new TThreadPoolServer(processor, serverTransport);
Console.WriteLine("Starting the server...");
server.Serve();
}
catch (Exception x)
{
Console.WriteLine(x.StackTrace);
}
}
private bool forceStop;
public int TimeConsumingOperation(int n1, int n2)
{
Console.WriteLine("add({0},{1})", n1, n2);
for (int i = 0; i < 10; i++)
{
//calculating
Thread.Sleep(500);
if (forceStop)
{
Quit();
}
}
return n1 + n2;
}
public void Stop()
{
forceStop = true;
}
// Client
// Button#1 Click callback
private void Button_Start_Click()
{
client.TimeConsumingOperation(0,0);
}
// Button#2 Click callback
private void Button_Stop_Click()
{
client.Stop();
}
//
我在 java [https://chamibuddhika.wordpress.com/2011/10/02/apache-thrift-quickstart-tutorial/] 中找到了一些有用的示例。我已经尽力将non-block server的java代码转换成对应的C#代码,但是我发现C#中好像没有TNonblockingServerTransport。谁能帮我解决这个问题?
// Java Code
public class NonblockingServer {
private void start() {
try {
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
ArithmeticService.Processor processor = new ArithmeticService.Processor(new ArithmeticServiceImpl());
TServer server = new TNonblockingServer(new TNonblockingServer.Args(serverTransport).
processor(processor));
System.out.println("Starting server on port 7911 ...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
NonblockingServer srv = new NonblockingServer();
srv.start();
}
}
这个问题实际上有两个答案。
可能的实现
首先,您的设置存在缺陷。
// #Server#
// Server Set-Up
private bool forceStop;
public void Stop()
{
forceStop = true;
}
假设我们有两个客户端,都开始了新的计算。现在一位客户想要中止。会发生什么?
解决方案是以这样一种方式构建它:计算是一个单独的业务逻辑对象,在 TimeConsumingOperation()
实例化并通过某种方式提供给客户端,通常是通过返回某种 ID .
当客户端现在想要中止时,它会调用 Stop(calcualtionID)
。服务器端逻辑现在将调用路由到实现并触发任何中止机制,C# 可能是 CancellationToken
.
计算完成后,需要第三次调用从服务器端查询最终结果。请注意,我们仍然使用 TSimpleServer
并且它起作用的原因是因为我们通过 API 设计避免了阻塞调用。
非阻塞服务器
是的,C# 还没有实现。由于 Thrift 是开源的,这可能仅仅意味着到目前为止 运行 没有人进入该用例并且想花时间在实现上。这并不是说这样的用例可能不存在。
存在的是
- 线程和线程池服务器
Task.Run(() => { your code })
这可能有助于解决您的用例。此外,当与 ASP.NET 一起使用时,不需要非阻塞服务器,因为运行时已经为多个连接提供了足够的支持。
底线
有一些方法可以解决您遇到的限制。另一个替代方案可能是 become a contributor by porting one of the existing (e.g. Java) nonblocking implementation to NetStd(首选,因为 C# 和 NetCore 将在下一个版本中成熟为 "deprecated" 状态,并且最终都将被 NetStd 取代)