doucai5315 2017-11-28 14:48
浏览 84
已采纳

Javascript和Python - 最好的沟通方法

I am trying to make a html/javascript controlled raspberry pi robot.

So far, I have installed a LAMP server and I am hosting a local webpage with buttons which trigger events and sends commands to my robot via AJAX. AJAX calls a php script which calls a python script (no CGI) to control the robot.

This solution is currently working for me and I can control my raspberry pi hardware from html and simple send data back and forth.

Now what I want to do is, on my web page, have an on/off button which initializes and shuts down the robot, as well as having other events (currently measuring tilt angles from an accelerometer in an android device) to control the speed of the robot. This is where I am getting confused by the procedure of how things will work.

When I make an AJAX call to start the robot, that ajax call does not close until the python script ends (robot shuts off). So I have a python script running (start.py) which contains my robot object where the methods for changing the speed are stored. If I have seperate events in JS to change the speed (sends a call to changespeed.py), how can I change the properties of an object which is stored in a different python script (start.py) that is already running (See figure below)?

Possible solutions I have thought of thus far:

1) store the object somewhere in memory. Perhaps as a python object, or store its properties in a SQL database. This way, changespeed.py (or Javascript directly) can change the speed properties in that memory, and start.py can continuously read them.

2) Rather than having the new speed values sent to python through AJAX, let python try to retrieve the values from javascript variables. in other words, instead of javascript giving data to python, have python take the data from javascript (scraping?).

3) abandon my current method of having javascript and python communicate through AJAX and php, and set up a websocket to exchange data. I dont know how this works quite yet but it may be the solution I need.

4) I also have not done any programming with interrupts and I dont know much about it. Is it possible to have my main script running and when the speed needs to change, I can send an interrupt at any moment?

I am sorry if my question is a little vague or wordy. I have a lot going on in my head right now. Let me know if it is unclear. Thank you.

TLDR: what is the best way to have javascript execute python scripts and then feed data to that script asynchronously

Map of operations

  • 写回答

3条回答 默认 最新

  • duanlang1196 2017-11-28 15:26
    关注

    You can use cgi — Common Gateway Interface and By the use cgi-cgi.FieldStorage() you can able to get data in python .it support Huge data transmission and fast.it spport all the browser

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd