Node.js长期任务


                    

我有一个node.js服务器,它正在从网络套接字通信到python套接字。 当用户使用数据发送异步ajax请求时,节点服务器将其传递给python,然后将数据返回给服务器,再从那里传递给客户端。</ p>

当用户发送ajax请求时会发生问题:他必须等待响应,并且如果python进程花费太多时间,那么ajax已经超时。</ p>

我试图在node.js中创建一个套接字服务器,并在python中使用要处理的数据连接到该套接字服务器的客户端。 节点服务器通过加载屏幕响应客户端。 处理完数据后,python套接字客户端将连接到node.js套接字服务器并传递处理后的数据。 但是,客户端无法请求已处理的数据,因为他不知道何时完成。</ p>
     </ div>

展开原文

原文

I have a node.js server which is communicating from a net socket to python socket. When the user sends an asynchronous ajax request with the data, the node server passes it to the python and gets data back to the server and from there to the client.

The problem occurs when the user sends the ajax request: he has to wait for the response and if the python process takes too much time then the ajax is already timed out.

I tried to create a socket server in node.js and a client that connects to the socket server in python with the data to process. The node server responds to the client with a loading screen. When the data is processed the python socket client connects to the node.js socket server and passes the processed data. However the client can not request the processed data because he doesn't know when it's done.

1个回答


因此,您具有三个系统和一个异步请求。 我最近使用PHP和box.com API解决了这样的问题。 PHP不允许无限期地保持连接打开,所以我遇到了类似的问题。</ p>

要解决该问题,我将使用递归请求。 它不是“实时”的,但这不太重要。</ p>

这是如何工作的:</ p>


  1. 客户端浏览器将“获取我的下载内容”请求发送到Node.js服务器。 Node.js服务器将唯一的请求ID返回到客户端浏览器。</ li>
  2. 客户端浏览器使用唯一的请求ID开始10秒钟的轮询,以查看是否有任何更改。 目前,答案是否定的。</ li>
  3. Node.js服务器收到此请求,并向Python服务器发送“去下载他的东西”请求。 (客户端浏览器仍每10秒轮询一次,答案仍然为否)</ li>
  4. python服务器实际上可以获取下载的东西,将其粘贴在一个地方,创建URL并将其返回给Node.js服务器。 (客户端浏览器仍每10秒轮询一次,答案仍然为否)</ li>
  5. Node.js服务器从Python服务器收到一条消息,其中包含该事物的URL。 它将URL与其开始的请求ID相对应。 此时,其状态更改为“是,我已经下载了文件,在这里!-URL)。</ p> </ li>

  6. 客户端浏览器接收到带有其URL的可爱数据包,立即停止轮询,并愉快地跳入日落。 (或类似的更合适的数字响应)。</ p> </ li>
    </ ol>

    希望这有助于您大致了解如何在不依赖推送技术的情况下解决此问题。 考虑根据下载时间的长短调整轮询间隔(我建议开始10秒)。 您甚至可能会变得棘手,等待30秒,然后每2秒轮询一次。 对其进行微调。</ p>
         </ div>

展开原文

原文

So you have three systems, and an asynchronous request. I solved a problem like this recently using PHP and the box.com API. PHP doesn't allow keeping a connection open indefinitely so I had a similar problem.

To solve the problem, I would use a recursive request. It's not 'real-time' but that is unlikely to matter.

How this works:

  1. The client browser sends the "Get my download thing" request to the Node.js server. The Node.js server returns a unique request id to the client browser.
  2. The client browser starts a 10 second poll, using the unique request id to see if anything has changed. Currently, the answer is no.
  3. The Node.js server receives this and sends a "Go get his download thing" request to the Python server. (The client browser is still polling every 10 seconds, the answer is still no)
  4. The python server actually goes and gets his download thing, sticks it in a place, creates a URL and returns that to the Node.js server. (The client browser is still polling every 10 seconds, the answer is still no)
  5. The Node.js server receives a message back from the Python server with the URL to the thing. It stores the URL against the request id it started with. At this point, its state changes to "Yes, I have your download thing, and here it is! - URL).

  6. The client browser receives the lovely data packet with its URL, stops polling now, and skips happily away into the sunset. (or similar more appropriate digital response).

Hope this helps to give you a rough idea of how you might solve this problem without depending on push technology. Consider tweaking your poll interval (I suggested 10 seconds to start) depending on how long the download takes. You could even get tricky, wait 30 seconds, and then poll every 2 seconds. Fine tune it to your problem.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐