Scene: Java call python, somewhat unstable, sometimes very fast, sometimes slow.
To ensure no contamination of the original code based on the asynchronous method call:
Service Code:
public interface AsyncService {
/**
* 异步执行pathon
* @param cmdArr
* @return
*/
void Async(String[] cmdArr);
}
import com.atage.op.service.AsyncService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* <p>
* 服务实现类
* </p>
*
* @author 鲁达
* @since 2019-09-05
*/
@Service
public class AsyncServiceImpl implements AsyncService {
@Async
@Override
public void Async(String[] cmdArr) {
try {
//要执行的业务代码
Runtime.getRuntime().exec(cmdArr);
}catch (Exception ex){
ex.printStackTrace();
}
}
}
Controller Code:
类上加注解:@EnableAsync
@Autowired
private AsyncService asyncService;
调用:
try {
asyncService.Async(cmdArr);
}catch(Exception e) {
throw new RuntimeException("业务程序报错啦!!");
}