[英]Running a Python script from Spring-Boot Web Application on embedded Tomcat
[英]Running a Python script from a Spring @Service
我需要使用從用戶界面傳遞的參數執行python腳本並顯示結果。 我知道如何使用ProcessBuilder(如下)進行此操作,但我認為僅從相關的Spring @Service調用此代碼不是一個好主意(線程問題,並發運行的實例過多,等等)。 最好的方法是什么?
@Override
public String executeLatestAlgorithm(String json) {
try {
ProcessBuilder probuilder = new ProcessBuilder("somescript.py", json);
Process p = probuilder.start();
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
return in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
(在沒有任何重大錯誤處理的情況下刪除代碼-僅用於說明)
非常感謝,
一種
顯然,Spring Integration提供了執行以Python,JS,Groovy等編寫的腳本的支持。
https://github.com/spring-projects/spring-integration-samples/tree/master/applications/cafe-scripted
與Python相關的配置XML的一部分如下所示
<service-activator input-channel="hotDrinks"
output-channel="preparedDrinks">
<script:script lang="python" location="file:scripts/python/barista.py">
<script:variable name="timeToPrepare" value="5" />
</script:script>
</service-activator>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.