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 8033813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:49:09+00:00 2026-06-05T01:49:09+00:00

I wrote a script that accessed a bunch of servers using nc on the

  • 0

I wrote a script that accessed a bunch of servers using nc on the command line, and originally I was using Python’s commands module and calling commands.getoutput(). The script ran in about 45 seconds. Since commands is deprecated, I want to change everything over to using the subprocess module, but now the script takes 2 min 45 secs to run. Why would this be?

What I had before:

output = commands.getoutput("echo get file.ext | nc -w 1 server.com port_num")

Now I have

p = Popen('echo get file.ext | nc -w 1 server.com port_num', shell=True, stdout=PIPE)
output = p.communicate()[0]
  • 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-06-05T01:49:11+00:00Added an answer on June 5, 2026 at 1:49 am

    I would expect subprocess to be slower than command. Without meaning to suggest that this is the only reason your script is running slowly, you should take a look at the commands source code. There are fewer than 100 lines, and most of the work is delegated to functions from os, many of which are taken straight from C POSIX libraries (at least in POSIX systems). Note that commands is Unix-only, so it doesn’t have to do any extra work to ensure cross-platform compatibility.

    Now take a look at subprocess. There are more than 1500 lines, all pure Python, doing all sorts of checks to ensure consistent cross-platform behavior. Based on this, I would expect subprocess to run slower than commands.

    I timed the two modules, and on something quite basic, subprocess was almost twice as slow as commands.

    >>> %timeit commands.getoutput('echo "foo" | cat')
    100 loops, best of 3: 3.02 ms per loop
    >>> %timeit subprocess.check_output('echo "foo" | cat', shell=True)
    100 loops, best of 3: 5.76 ms per loop
    

    Swiss suggests some good improvements that will help your script’s performance. But even after applying them, note that subprocess is still slower.

    >>> %timeit commands.getoutput('echo "foo" | cat')
    100 loops, best of 3: 2.97 ms per loop
    >>> %timeit Popen('cat', stdin=PIPE, stdout=PIPE).communicate('foo')[0]
    100 loops, best of 3: 4.15 ms per loop
    

    Assuming you are performing the above command many times in a row, this will add up, and account for at least some of the performance difference.

    In any case, I am interpreting your question as being about the relative performance of subprocess and command, rather than being about how to speed up your script. For the latter question, Swiss’s answer is better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a Python script that does some task to generate, and then keep
I'm using Dynatree for the first time and wrote a PHP script that returns
I wrote Python script that processes big number of large text files and may
I wrote a script that takes a bunch of options ( -d , -v
I wrote a script that decodes aydio using FFmpeg. Here's the code: JNIEXPORT jint
I wrote a script that is using the FilterIterator class that comes from the
I recently wrote a script that does a sed command, to replace all the
I wrote a script that uses xcodebuild to generate an AdHoc build of an
I wrote a simple script that is intended to do hierarchical clustering on a
I wrote a Perl script that it makes some SQL queries in a table

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.