FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Synchronized execution of python script · Issue #170 · extrabacon/python-shell · GitHub

Repository navigation

Synchronized execution of python script #170

Description

Is there way trun python script in sync with node

function() {
   let pyshell = new PythonShell('.myscript.py')
let content = ""
    pyshell.on('message',function(message){
        content = message
    })
return content // content needs to be message instead due to async nature it returns empty string
}

Activity

  1. Almenon commented on Jan 24, 2019

    Collaborator

    Look at the readme - you want the run command.

  2. dhaval-lila commented on Jan 25, 2019

    Author

    I am guessing you meant PythonShell.run() that one too is async function. It takes callback function.

  3. Almenon commented on Jan 25, 2019

    Collaborator

    Oh yeah you're right, sorry. We don't have a synchronous option but that's a good idea

  4. dhaval-lila commented on Jan 25, 2019

    Author

    If you are looking to add this feature , I will gladly help and I was looking to contribute and this is great opportunity for my first contribution I am familiar with node and related web technologies so can you guide me through what i need to know to implement synchronous option

  5. Almenon commented on Jan 25, 2019

    Collaborator

    Thanks! For sychronus operation first we need to migrate to using promises (read #77). I've created a v2 branch for this: https://github.com/extrabacon/python-shell/tree/version2

    Once we are using promises then we can make sync versions of our functions - see this article: https://medium.com/@patarkf/synchronize-your-asynchronous-code-using-javascripts-async-await-5f3fa5b1366d

  6. WingDust commented on Jan 10, 2020

    I want to ask don't it already come true

  7. abrichr commented on Feb 4, 2020

    +1

  8. Almenon commented on Feb 4, 2020

    Collaborator

    re-posting my comment from #77:

    To be honest I'm probably not going to get to this anytime soon. I'd be happy to review a PR if someone submits it. Or if someone wants to donate to charity I can use that as motivation to get around to implementing this.

  9. nkhil commented on Feb 21, 2020

    We had the same issue today, and ended up promisifying it like so:

    function someFunction(date, data){
      return new Promise((resolve, reject) => {
        let result;
        let pyshell = new PythonShell('pyshell_test.py', {mode: 'text', args: [date]});
        
        pyshell.send(JSON.stringify(data['someProperty']));
        
        pyshell.on('message', function (message) {
          result = JSON.parse(message);
        });
        
        pyshell.on('stderr', function (stderr) {
          console.log(stderr);
        });
        
        pyshell.end(function (err, code, signal) {
          if (err) reject(err);
          console.log('The exit code was: ' + code);
          console.log('The exit signal was: ' + signal);
          console.log('finished');
          resolve(result);
        });
        
      });
    }
  10. karanbalani commented on May 1, 2021

    @nkhil this works (at least for now), thanks. :)

  11. AlexString commented on Apr 2, 2022

    @nkhil implementation worked for me but I did something more like this in a Class:

    callPythonScript = () => {
    	return new Promise((resolve, reject) => {
    		PythonShell.run(this.pythonScriptName, this.options, (err, result) => {
    			if (err) reject(err);
    
    			resolve(result);
    		});
    	});
    };
  12. dschweinbenz commented on Jan 10, 2023

    @nkhil implementation worked for me but I did something more like this in a Class:

    callPythonScript = () => {
    	return new Promise((resolve, reject) => {
    		PythonShell.run(this.pythonScriptName, this.options, (err, result) => {
    			if (err) reject(err);
    
    			resolve(result);
    		});
    	});
    };

    One should use return reject(err) to avoid resolving the request afterwards. See https://stackoverflow.com/questions/32536049/do-i-need-to-return-after-early-resolve-reject

  13. dusan-maintains commented on Mar 10, 2026

    I'm actively maintaining this package and have an open PR (#320) fixing the runString() temp-path bug. I'm also tracking maintenance work publicly at dusan-maintains/oss-maintenance-log — all PRs and SLA are logged automatically every 6 hours.

    If you're hitting issues with synchronized execution, the underlying problem is often related to how temp files are handled. Happy to look into this further if you can share a minimal repro.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL