-
Notifications
You must be signed in to change notification settings - Fork 222
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
sending message to python from node.js using python-shell without terminating the python script #260
Comments
Try Print output in python is buffered by default. I suggest looking up python print buffering and reading up on it. |
this is what i had
and unfortunately, i still didn't see anything in the console while pyshell.end function was commented out. i will read print buffering though |
I'm not at the computer at the moment so I can't test it but I suspect the
problem is that sys.stdin.readlines() blocks until stdin is closed. Try
reading the input in a different way.
…On Thu, Nov 18, 2021, 9:12 AM 8ahmedanwer8 ***@***.***> wrote:
this is what i had
def main():
#get our data as an array from read_in()
lines = sys.stdin.readlines()
lines = json.loads(lines[0])
total_sum_inArray = 0
for item in lines:
total_sum_inArray += item
print (lines,flush=True) //modification here
and unfortunately, i still didn't see anything in the console while
pyshell.end function was commented out
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#260 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADDZTBMSSBIK2XXIOZYQBITUMUXY7ANCNFSM5IH7AUJQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
isn't the only way to read input with pyshell is to use sys.stdin? and stdin will remain open until pyshell does pyshell.end which ends the script. so unless the script ends, communication won't work? sorry, just trying to wrap my head around this. |
There are several ways of reading from stdin. For example you could use the
input() method which reads one line at a time. You could also iterate over
stdin.readlines in a loop.
…On Mon, Nov 22, 2021, 11:39 AM 8ahmedanwer8 ***@***.***> wrote:
isn't the only way to read input with pyshell is to use sys.stdin? and
stdin will remain open until pyshell does pyshell.end which ends the
script. so unless the script ends, communication won't work? sorry, just
trying to wrap my head around this.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#260 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADDZTBM7CZLVUHMWNGTAGZ3UNKL77ANCNFSM5IH7AUJQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Also note that the input method is built into python, you don't need any
imports to use it. The input method is what beginners usually use.
On Tue, Nov 23, 2021, 8:44 AM Caleb Sparks ***@***.***>
wrote:
… There are several ways of reading from stdin. For example you could use
the input() method which reads one line at a time. You could also iterate
over stdin.readlines in a loop.
On Mon, Nov 22, 2021, 11:39 AM 8ahmedanwer8 ***@***.***>
wrote:
> isn't the only way to read input with pyshell is to use sys.stdin? and
> stdin will remain open until pyshell does pyshell.end which ends the
> script. so unless the script ends, communication won't work? sorry, just
> trying to wrap my head around this.
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <#260 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ADDZTBM7CZLVUHMWNGTAGZ3UNKL77ANCNFSM5IH7AUJQ>
> .
> Triage notifications on the go with GitHub Mobile for iOS
> <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
> or Android
> <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
>
>
|
so i made the following changes to the python and it worked!:
now, i added the shell.on and shell.send functions inside an event listener that communicates with the front-end, and I found that the python script only runs once.
so above, i see my output from python, done message and then only done message logged after that when the event is fired by a button |
You might wanna try this:
|
i am doing some communication between electron.js/node.js and python. i can run a file from python and send a message to it, which is capture via stdin, and that python file can print my message to the electron console. but, this only seems to work if i terminate the program after sending my message in node whereas, i want send the message while the program is running.
my output is [1,2,3,4,5] as it should be, but when i remove pyshell.end listener in the node code, i don't get any output in the console. i'm not sure how sys and stdin exactly work so maybe that's why i cannot figure this out. also, this is my first time asking a question on github so a bit nervous hehe.
thanks for any guidance.
The text was updated successfully, but these errors were encountered: