多个客户端对服务器的函数调用:隔离每个客户端调用
我的项目是独立的应用程序,然后我决定将其拆分为客户端和客户端。服务器,因为我同时需要强大的CPU利用率和可移植性。现在多个客户端可以连接到一台服务器。
当一对一处理完成这项工作时,这很容易。现在我需要调用相同的函数 &再次范围区域&同时再次 - 通过客户端请求 -
请任何人给我一些线索,我应该如何处理这些操作,我需要知道如何在服务器端将客户端进程彼此隔离?我的通信是异步的,服务器接收请求并启动一个新线程。我想我传递了一个参数,其中一个参数携带客户端信息,另一个参数作为作业 ID - 为了帮助客户端返回,客户端可能会要求多个作业,并且某些作业比其他作业完成得更快
- 我应该在每次调用时实例化类 Process 吗?我可以使用静态方法等吗?任何解释都会有很大帮助!
以下是我的代码中需要修改的部分
class static readonly Data
{
public variable listOfValues[]
}
class Process
{
local variable bestValue
function findBestValue(from, to)
{
...
if(processResult > bestValue) bestValue = processResult
...
}
...
for(i=0;i<10;i++) startThread(findBestValue(i*1000,i*1000+999));
...
}
编辑:我想我必须实例化一个 新的 Process 类并调用 为每个客户端提供函数并忽略 由于作业已经在运行,因此同一作业的同一客户端。
My project was standalone application then I decided to split it as client & server because I need powerful CPU usage and portability at the same time. Now multiple clients can connect to one server.
It was easy when 1 by 1 processing did the job. Now I need to call the same function & scope area again & again at the same time -via client requests-
Please can anyone give me some clue how should I handle these operations, I need to know how can I isolate clients' processes from each other at the server side? My communication is asynchronous, server receives a request and starts a new thread. I think I pass a parameter which one carries the client information, and another parameter as job id -to help client back, client may ask for multiple jobs and some jobs finish quicker than others-
Should I instantiate the class Process on each call? Can I use a static method, etc, any explanation will be of great help!
Below is the part of my code to need modification
class static readonly Data
{
public variable listOfValues[]
}
class Process
{
local variable bestValue
function findBestValue(from, to)
{
...
if(processResult > bestValue) bestValue = processResult
...
}
...
for(i=0;i<10;i++) startThread(findBestValue(i*1000,i*1000+999));
...
}
EDIT: I think I have to instantiate a
new Process class and call the
function for each client and ignore
the same client for same job since job is already running.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有进入你的应用程序设计,因为你没有谈论太多,我认为你的问题非常适合使用 WCF WebServices。您通过设计获得客户端隔离,因为每个请求都将在其自己的线程中启动。您可以将 WCF 主机创建为独立应用程序/Windows 服务。
Not getting into your application design, since you didn't talk much about it, I think that your problem is ideal for using WCF WebServices. You get client isolation by design because every request will start in it's own thread. You can create WCF host as standalone application/windows service.
您可以使用
WCF
服务包装您的通信,并将其配置为PerCall
服务(这意味着每个请求将与其他请求分开处理)。因此,您将清除同步内容中的业务逻辑。这是最好的方式,因为管理和创建线程并不难实现,但很难正确实现并针对资源消耗进行优化。
You can wrap your communication with
WCF
service and configure it to bePerCall
service (meaning each request will be processed separately from others).So you'll clean up your buisness logic from syncronization stuff. That's the best way, because managing and creating threads is not difficult to implement, but it is difficult to implement correctly and optimized for resources consumption.