#Tauri sidecar issue: No output received

39 messages · Page 1 of 1 (latest)

gentle sentinel
#

Hi I am trying to get my tauri to work with a python script as a sidecar. Reason -> I want to run a python script that uses the users webcam to detect gestures to control the app.

To make it easy, I started with a sample project.

Pyhton script:

import sys 
import time

while True:   
    sys.stdout.write('Hello from python script from stdout')
    print("Hello from python script from console")
    time.sleep(1)

Converted it to a binary and followed the docs https://tauri.app/v1/guides/building/sidecar/

Src-tauri/main.rs:

// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::{
  api::process::{Command, CommandEvent}
};

fn main() {
  tauri::Builder::default()
  .setup(|_app| {
    tauri::async_runtime::spawn(async move {
      let (mut rx, _child) = Command::new_sidecar("main")
        .expect("failed to setup `app` sidecar")
        .spawn()
        .expect("Failed to spawn packaged node");

      while let Some(event) = rx.recv().await {
        if let CommandEvent::Stdout(line) = event {
          println!("{}", format!("'{}'", line));
        }
      }
    });

    Ok(())
  })
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

I am not getting any error message at all so I assume it runs the sidecar but I am not getting any output from the sidecar. Any reason why?

You may need to embed depending binaries to make your application work or prevent users from installing additional dependencies (e.g., Node.js or Python).

molten roost
#

how does the tauri.conf.json and Cargo.toml looks like.

how you invoke your command ... there some stuff missing.

gentle sentinel
#

Tauri.conf.json is like exacly like the docs and the cargo.toml , there is nothing that would change the situation, I wouldnt know what you are looking for there. It is a barebone tauri app

molten roost
#

sooo .... "binaries/main" is your python binary ?

and you need to add a feature in your cargo.toml

#

below "Running it from Rust"

Note that you must enable the process-command-api Cargo feature (Tauri's CLI will do this for you once you changed the config):

#

you need to call the command

like

import { Command } from '@tauri-apps/api/shell'

const command = Command.sidecar('binaries/main', [some, args]) // <- Select your python binarie you included
const output = await command.execute() // execute it.
gentle sentinel
#

But I dont want to run it from the front end

#

I want to run in in the setup function of the tauri backend

#

@molten roost I indeed needed to at process-command-api to cargo.toml but it is still not working when I call it from rust

molten roost
#
      let (mut rx, _child) = Command::new_sidecar("main")
        .expect("failed to setup `app` sidecar")
        .args(["arg"])
        .spawn()
        .expect("Failed to spawn packaged node");
gentle sentinel
#

But I have no arguments to give it

#

@molten roost Can you reproduce the issue with my github repository?

#

@molten roost Okay so

#

I let the app run for a bit and after a while I get my output

#

But it is not instantaneous. Why is that ?

molten roost
#

you spawn a process and run it ... it takes a bit

gentle sentinel
#

it outputs like 20 console log at once

#

then it stops

#

then after a while again 20 console log at once

#

although my python script always waits for 1 second and then sends it

molten roost
#

overall you setup your app ... and when app is running your spawn the process ... thats why it takes time

#

20 console log ..... maybe because of your while loop in the python script ?

gentle sentinel
#

The issue is not the start up time. I am guessing that it has to do something with the while loop

#

I seems that tauri is ready the console logs from my python script but only outputs them when their buffer is full or something

#

That is why it takes a while and I get everything at once and then wait and then again everything at once, over and over

#

thats my guess

#

but now the question is why and how can I prevent this and get live the outputs from the python script

tender siren
#

can you try explictly writing a new line in your pythong code

#

like using your code from above: ```py
while True:
sys.stdout.write('Hello from python script from stdout\n')
print("Hello from python script from console\n")
time.sleep(1)

#

print should already include one but stdout.write probably doesn't. either way, try it with both

gentle sentinel
#

@tender siren Okay I will try and indeed the sys.stdout.write is not working, the print is enough.

#

Still no succes :/

#

I still receive everything in chunks

molten roost
#

write them to a buffer and then read the buffer

gentle sentinel
#

You have an example how?

#

@nocturne veldt You have any experience with this? I saw your youtube video about sidecarts.

gentle sentinel