简体   繁体   English

Javascript-Python:向客户端浏览器提供动态生成的图像?

[英]Javascript-Python: serve dynamically generated images to client browser?

Scenario: User loads a page, image is being generated, show loading bar, notification event sent to browser. 场景:用户加载页面,生成图像,显示加载栏,发送到浏览器的通知事件。

I am using python code to generate the image. 我正在使用python代码生成图像。 Would it be ideal to have a web server that launches the script or embed a webserver code into the python script? 拥有一个启动脚本或将网络服务器代码嵌入python脚本的Web服务器是否理想? Once the image is finished rendering, the client should receive a message saying it's successful and display the image. 图像完成渲染后,客户端应收到一条消息,说明它成功并显示图像。

How can this be architected to support concurrent users as well? 如何构建它以支持并发用户? Would simply launching the python script for each new user that navigates to the web page suffice? 只需为导航到网页的每个新用户启动python脚本就足够了吗?

Would it be overkill to have real-time web application for this scenario? 为这种情况提供实时Web应用程序会有点难过吗? Trying to decide whether simple jQuery AJAX will suffice or Socket.io should be used to have a persistent connection between server and client. 试图决定简单的jQuery AJAX是否足够,或者应该使用Socket.io在服务器和客户端之间建立持久连接。

Any libraries out there that fit my needs? 那里有适合我需求的图书馆吗?

I personally love Socket.IO and I would to it with it. 我个人喜欢Socket.IO,我会喜欢它。 Because it would be simpler a way. 因为它会更简单。 But that may be a bit too much work to set up just for that. 但是,为此设置可能需要做太多工作。 Especially since it is not that simple in Python from what I heard compare to node where it really is about 10 lines server side. 特别是因为它在Python中并不像我所听到的那样简单,与节点相比,它实际上是10行服务器端。

Without Socket.IO could do a long polling to get the status of the image processing, and get the url at the end, of the image in base64 if that is what you want. 没有Socket.IO可以做一个长轮询来获取图像处理的状态,并获得base64中图像的最终网址,如果这是你想要的。

I would recommend not spawning a new child for every request, but rather using some task queue management tool, for example I can reccomend celery . 我建议不要为每个请求生成一个新的子项,而是使用一些任务队列管理工具,例如我可以推荐celery

You could for example run a threaded python webserver which would spawn one task for each request. 例如,您可以运行一个线程python webserver,它将为每个请求生成一个任务。 Each client could then ask periodically (javascript/jquery) if the task is finished, and if so, server would return proper image address. 然后,每个客户端可以定期询问(javascript / jquery)任务是否完成,如果是,服务器将返回正确的图像地址。 Also, celery has web-callbacks which could be used in this case. 此外,芹菜还有网络回调 ,可以在这种情况下使用。 (I think, I have not used this feature myself). (我想,我自己没有使用过这个功能)。

I would not lose time with writing some ad-hoc solution, since this is simple, easy to deploy, well documented and scales very well. 我不会浪费时间编写一些临时解决方案,因为这很简单,易于部署,记录良好并且可以很好地扩展。

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

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