Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7010903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:03:33+00:00 2026-05-27T22:03:33+00:00

This Python code pipes data through Perl script fine. import subprocess kw = {}

  • 0

This Python code pipes data through Perl script fine.

import subprocess
kw = {}
kw['executable'] = None
kw['shell'] = True
kw['stdin'] = None
kw['stdout'] = subprocess.PIPE
kw['stderr'] = subprocess.PIPE
args = ' '.join(['/usr/bin/perl','-w','/path/script.perl','<','/path/mydata'])
subproc = subprocess.Popen(args,**kw)
for line in iter(subproc.stdout.readline, ''):
    print line.rstrip().decode('UTF-8')

However, it requires that I first to save my buffers to a disk file (/path/mydata). It’s cleaner to loop through the data in Python code and pass line-by-line to the subprocess like this:

import subprocess
kw = {}
kw['executable'] = '/usr/bin/perl'
kw['shell'] = False
kw['stderr'] = subprocess.PIPE
kw['stdin'] = subprocess.PIPE
kw['stdout'] = subprocess.PIPE
args = ['-w','/path/script.perl',]
subproc = subprocess.Popen(args,**kw)
f = codecs.open('/path/mydata','r','UTF-8')
for line in f:
    subproc.stdin.write('%s\n'%(line.strip().encode('UTF-8')))
    print line.strip()  ### code hangs after printing this ###
    for line in iter(subproc.stdout.readline, ''):
        print line.rstrip().decode('UTF-8')
subproc.terminate()
f.close()

The code hangs with the readline after sending the first line to the subprocess. I have other executables that use this exact same code perfectly.

My data files can be quite large (1.5 GB) Is there way to accomplish piping the data without saving to file? I don’t want to re-write the perl script for compatibility with other systems.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:03:34+00:00Added an answer on May 27, 2026 at 10:03 pm

    Thanks srgerg. I had also tried the threading solution. This solution alone, however, always hung. Both my previous code and srgerg’s code were missing the final solution, Your tip gave me one last idea.

    The final solution writes enough dummy data force the final valid lines from the buffer. To support this, I added code that tracks how many valid lines were written to stdin. The threaded loop opens the output file, saves the data, and breaks when the read lines equal the valid input lines. This solution ensures it reads and writes line-by-line for any size file.

    def std_output(stdout,outfile=''):
        out = 0
        f = codecs.open(outfile,'w','UTF-8')
        for line in iter(stdout.readline, ''):
            f.write('%s\n'%(line.rstrip().decode('UTF-8')))
            out += 1
            if i == out: break
        stdout.close()
        f.close()
    
    outfile = '/path/myout'
    infile = '/path/mydata'
    
    subproc = subprocess.Popen(args,**kw)
    t = threading.Thread(target=std_output,args=[subproc.stdout,outfile])
    t.daemon = True
    t.start()
    
    i = 0
    f = codecs.open(infile,'r','UTF-8')
    for line in f:
        subproc.stdin.write('%s\n'%(line.strip().encode('UTF-8')))
        i += 1
    subproc.stdin.write('%s\n'%(' '*4096)) ### push dummy data ###
    f.close()
    t.join()
    subproc.terminate()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've got this snazzy python code: import subprocess value = subprocess.Popen([php,./php/php_runner.php],stdout=subprocess.PIPE); the problem is,
/Users/smcho/Desktop/bracket/[10,20] directory has abc.txt, but when I run this Python code import glob import
I ran into unbound method error in python with this code: import random class
I have this Python code to do this: from struct import pack as _pack
Suppose I have this Python code: from itertools import count, tee original = count()
I've got running sample python code which is fine in Ubuntu desktop: import dbus,
Consider this Python code for printing a list of comma separated values for element
I was given this Python code that would calculate an MD5 value for any
I've inherited some python code that contains a rather cryptic decorator. This decorator sets
I occasionally see the list slice syntax used in Python code like this: newList

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.