Forums

BlockingIOError when making request in javascript subprocess

Hi, I am trying to use an npm library that also needs to make some api calls, so I need to be able to make post requests from my javascript files. I am using python 3.4.3, django 1.6.6 and node.js 8.10.0. What I am trying to do is use subprocess.Popen in my routes to call some javascript methods and everything works fine until I make "callback-requests" to return a value from javascript back to python, because when I do, I get "BlockingIOError [Errno 11] write could not complete without blocking".

So I call js from python like this(I simplified the string):

p = subprocess.Popen("node -e 'require("script.js").doAsynchronousStuff(callbackRoute)'",
cwd=project_folder, shell=True, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

retval = p.wait()

And my .js function looks about like this:

function doAsynchronousStuff(callbackRoute) {
    const data = ...;
    const params = {
        uri: callbackRoute,
        body: JSON.stringify(data),
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        }
    };
    request(params, (error, response) => {
        console.log(error, response);
    });
}
module.exports.doAsynchronousStuff = doAsynchronousStuff;

Some things I noticed is that I can always call the function from my bash console without issues and that it sometimes even works when I use my python routes, but most of the time it doesn't. If I was simply not allowed to make a request to django, while my subprocess is still running, then it should never work, right? This is the best way I could think of to use the library, but suggestions to different approaches are welcome.

Edit: Sorry, the preview in this editor doesn't look like the final version, I don't get it

[edit by admin: code formatting]

I'm wondering if it's something to do with the amount of output that the JavaScript code is printing out. What happens if you use subprocess.check_output rather than Popen then wait?