可以在Java和Python之间同步线程吗?

huangapple go评论58阅读模式
英文:

Is it possible to syncronize threads between java and Python?

问题

我有一个用Java编写的服务器。它接受HTTP请求作为执行工作的命令。这项工作需要时间。

我们在后端使用Java,在前端使用Python发送HTTP请求并控制工作的执行。在Python中,存在一个threading.Event,允许线程之间进行通信。我想要做的事情可能因为我们选择的语言而变得困难,但是是否可能在Java和Python之间实现相同类型的通信?

也就是说,Java创建事件并提供一个指针,Python可以访问并通过set方法设置它吗?我曾在C++和Python之间使用过这种模式,但我猜那可能更容易,因为Python是用C编写的。

欢迎提供任何帮助或指向资源的建议,谢谢。

英文:

I have a server written in Java. It accepts http requests as commands to do work. The work takes time.

We use Java in the backend and Python in the front end sending the http requests and controlling the work done. In Python, there exists athreading.Event which allows communication between threads. What I would like to do might be difficult because of our language choice, but is it possible to have the same type communication between java and Python?

That is, Java creates the event and provides a pointer to it which can be accessed and set by Python? I've used this pattern between C++ and Python, but I guess that's easier because Python is written in C.

Any help or pointers to resources will be welcome, thanks.

答案1

得分: 2

我建议您使用基于http的回调机制。

在Python应用程序中公开一个http post端点,接受调用,指示Java服务已完成其工作。

Java调用Python以指示完成并传回结果。通常,Python首次调用Java时包括用于识别调用的信息,并在回调中传回此标识符,以便Python将所有内容绑定在一起。

此方法的优点在于,它使用http进行所有集成,不受任何一端的实现技术的限制。它允许可伸缩性、位置独立性和微服务类型的架构。

实现说明

Python首次调用Java的负载必须包括Python的http服务器运行的url,以便Java在完成工作时回调Python。例如,如果我们将POST请求发送到Java:

{
    “callbackUrl”: “http://10.0.0.2:8000/callback?job_id=123”,
    “foo”: “bar”
}

完成后,Java(java.net.http.HttpClient或Spring RestTemplate)将结果以POST方式发送到提供的callbackUrl,并标识作业(如果需要)。

POST http://10.0.0.2:8000/callback?job_id=123
{
    “resultFoo”: true,
    “resultBar”: 10
}

在Python中创建一个函数来接受回调,例如使用 flask

@api.route('/callback', methods=['POST'])
def post_callback():
    
  jobId = request.args.get('job_id')
  # 链接回原始调用,带有JobId

  request_data = request.get_json()
  # 使用已发布的请求主体数据,例如 request_data.resultFoo

  return json.dumps({"success": True}), 201)
英文:

I suggest that you use an http-based callback mechanism.

Expose an http post endpoint in the Python application that accepts a call indicating that the Java service has completed its work.

Java calls Python back to indicate completion and pass back results. Usually the initial call that Python makes to Java includes info to identify the call and this identifier is passed back in the callback allowing Python to tie everything together.

The advantage of this approach is that, because it uses http for all integration, it is not tied to the implementation technology on either end. It allows scalability, location independence and a microservice type architecture.

Implementation notes

The payload in initial call from Python to Java must include a url where the Python http server is running, for Java to call Python back on when it’s completed the work. For example the following request body (if we are POSTing to Java):

{
    “callbackUrl”: “http://10.0.0.2:8000/callback?job_id=123”,
    “foo”: “bar”
}

When complete, Java (java.net.http.HttpClient or Spring RestTemplate) POSTs the results to the callbackUrl provided and identifies the job (if required.)

POST http://10.0.0.2:8000/callback?job_id=123
{
    “resultFoo”: true,
    “resultBar”: 10
}

In Python create a function to accept the callback, eg with flask

@api.route('/callback', methods=['POST'])
def post_callback():
    
  jobId = request.args.get('job_id')
  #link back to original call with JobId

  request_data = request.get_json()
  # used the posted body data, eg request_data.resultFoo

  return json.dumps({"success": True}), 201)

答案2

得分: 0

我认为这是一个非常常见的问题,应该更好地表述为“后端如何通知前端某些更改”。因此,此问题以前已经被提出过。以下是其中一个具有良好答案的问题:如何在后端发生更改时持续向前端发送数据

英文:

I think that this is a very common question that should be better formulated as "how can backend notify frontend on some changes". So this question has been asked before. Here is one of those questions with good answer to it: How to continues send data from backend to frontend when something changes

huangapple
  • 本文由 发表于 2023年4月17日 16:34:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定