Python Forum
Simple inter-process communication: Switching from Perl to Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple inter-process communication: Switching from Perl to Python
#1
My go-to scripting language for quite some time has been perl.

However, I have been keenly interested in Python and I recently implemented a few projects in both Python 2 and 3.

I'm currently in the analysis stage of a project that requires 2-way interprocess communication with a C executable over which I have no control.

The executable is an interpreter that accepts unbuffered, newline-terminated "commands" from stdin and prints unbuffered, newline-terminated output to stdout and does not support socket communications.

The goal is to find a "minimalist" python 3 equivalent to perl's open2 function illustrated by the runnable perl script shown below


Google searching resulted in two notable options:
  • The multiprocessing module: After a quick look at examples it appears to me that's a bit of overkill for my use case.

  • ipc from PyPI: Looks promising but I could not locate any documentation for it and it provides no documentation in the code itself.

Any advice, pointers and/or sample code would be greatly appreciated.


Example perl code. (Should be runnable on a unix host)

Quote:#!/bin/bash
perl -e '
use FileHandle;
use IPC::Open2;

# Chat with the unix "cat" utility. :)
$pid = open2(*Reader, *Writer, "cat");

print Writer "Hello\n"; # Send
print scalar(<Reader>); # Print response

print Writer "World\n"; # Send
print Writer "Thats all folks!\n";
print Writer "The End\n"; # Send

while(<Reader>) { # Print responses
print;
if (/End/) {exit}
} '


The above script, not unexpectedly, produces the following output:

Output:
Hello World Thats all folks! The End
Reply
#2
I just found this page https://pymotw.com/3/subprocess/index.html about the subprocess module and I think that'll answer my question!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie question about switching between files - Python/Pycharm Busby222 3 594 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Question about Creating an Automated Process in Python Supratik1234 0 741 Jan-13-2023, 08:29 PM
Last Post: Supratik1234
  Help Switching between keyboard/Mic input in my code Extra 1 1,084 Aug-28-2022, 10:16 PM
Last Post: deanhystad
  Python multiprocessing Pool apply async wait for process to complete sunny9495 6 6,406 Apr-02-2022, 06:31 AM
Last Post: sunny9495
  Process the image on the Python HTTP server Aleks 0 3,199 Dec-02-2021, 11:43 PM
Last Post: Aleks
  Good Example for bi-directional Communication between Python and Windows (C#) raybowman 0 1,596 Nov-14-2020, 06:44 PM
Last Post: raybowman
  How to to tie the execution of one process to another inside a loop in Python ignorant_wanderer 0 2,044 Jul-11-2020, 03:44 AM
Last Post: ignorant_wanderer
  switching from pycharm to sublime issue Drifter 2 1,988 Jun-09-2020, 09:19 PM
Last Post: Drifter
  win32 API: Launch Application to RDP session from background process python script rangeshgupta 0 2,140 May-28-2020, 09:41 PM
Last Post: rangeshgupta
  Spawning a new process that is not attached to python cman234 3 1,891 Apr-25-2020, 05:24 PM
Last Post: cman234

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020