Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continuous data transfer from javascript to python and vice versa without ending the python process. #248

Open
prince381 opened this issue Jul 26, 2021 · 1 comment
Labels

Comments

@prince381
Copy link

The Question:
I have a nuxt-electron application that I want to use python as my backend. I have a python script that is supposed to run forever and continuously read data from javascript using python-shell. How can I configure python-shell to run my python script without ending the process.

My javascript code in electron's main.js

let python = new PythonShell('./python/client.py',{mode: 'text'});

ipcMain.on('send-msg',(event,data) => {
  let win = BrowserWindow.fromWebContents(event.sender);
      
  python.send(data)
  python.on('message',(msg) => {
     win.send('recv-msg',msg)
  });

  python.end(err => {
     if (err) console.log(err)
  });
})

In my python application

import sys

resp = sys.stdin.read()

while True:
    print(resp)
    resp = sys.stdin.read()
@f-o-w-l
Copy link

f-o-w-l commented Aug 3, 2021

Just a guess as I haven't actually used this library yet, but it looks like you should try moving your python.end(... call to a different ipcMain listener, something like below, while leaving the rest of your code in the ipcMain.on('send-msg'... as it is.

ipcMain.on('closed', () => {
  mainWindow = null;

  python.end(err => {
    if (err) console.log(err);
  });

  if (process.platform !== 'darwin') {
    app.quit();
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants