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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘