简体   繁体   English

不要让Laravel / PHP等待python脚本完成

[英]Don't make Laravel / PHP wait for python script to finish

Current setup 当前设置

I currently have a controller class called HomeController.php creates a request for the details the user enters in a form as well as saving other details (Which is not part of this problem). 我目前有一个名为HomeController.php的控制器类,它为用户以表格形式输入的详细信息创建request ,并保存其他详细信息(这不是此问题的一部分)。

This request is then sent to another controller class Jobcontroller.php which runs the API, careerjet and returns urls 然后,此request将发送到另一个控制器类Jobcontroller.php ,该类运行API, careerjet并返回urls

The problem 问题

I have to manually need to visit another page to run an the API and get the results, then having to execute the python scripts. 我必须手动访问另一个页面以运行API并获取结果,然后必须执行python脚本。 IE: IE浏览器:

website.co.uk/output -> URL 1 , URL 2 , URL 3
exec(script1.py)
exec(script2.py)
exec(script3.py)

What i'm trying to achieve 我正在努力实现的目标

I do not want to do this of course, and need everything to be automated. 我当然不希望这样做,并且需要使一切自动化。 Meaning, if the user clicks submit , the api code will immediately run and then the python scripts and it shouldn't affect the front-end usability speed. 意思是,如果用户单击submit ,则api代码将立即运行,然后运行python脚本并且不会影响前端的可用性速度。 (They don't have to linger on the same page, waiting for the last job to finish). (他们不必在同一页面上徘徊,等待上一份工作完成)。

I looked at queues implementation for this - and it seems that it only runs when theres no other requests. 我为此目的研究了队列实现-似乎它仅在没有其他请求时运行。

This is more of an architecture type of question, but you can try these options: 这更多是关于架构类型的问题,但是您可以尝试以下选项:

1. Create a Separate Python Application and communicate via an API 1.创建一个单独的Python应用程序并通过API进行通信

If synchronous operation is mandatory, you may want to have your python scripts be executable via an API. 如果必须进行同步操作,则可能希望通过API执行python脚本。 Simply host your scripts via something like Flask. 只需通过Flask之类的主机脚本即可。

When Laravel sends an API request to the Flask application, it will kick off the script, and return the result via the HTTP API. 当Laravel向Flask应用程序发送API请求时,它将启动脚本,并通过HTTP API返回结果。

2. Execute the Job in a Queue 2.在队列中执行作业

So basically same flow as above, but actually create a Laravel Job that communicates to this Flask Application in the background . 基本上与上述流程相同,但是实际上创建了一个Laravel Job并在后台与该Flask应用程序进行通信。 The user can continue about their business in the Laravel application, when the Job is complete, you can update your database, send a notification, whatever. 用户可以在Laravel应用程序中继续其业务,完成Job后,您可以更新数据库,发送通知等。

3. Execute the Python Script via a PHP shell command 3.通过PHP Shell命令执行Python脚本

I don't recommend this approach for a long term solution, but you could have PHP execute a shell command that kicks off your python script. 我不建议长期使用此方法,但是您可以让PHP执行启动Python脚本的shell命令。

Having the script communicate back to the PHP application could be a total pain in the ass. 让脚本与PHP应用程序进行通信可能会很麻烦。 But it's possible. 但这是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM