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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:10:20+00:00 2026-05-27T18:10:20+00:00

I want to connect to multiple telnet hosts using threading in python, but I

  • 0

I want to connect to multiple telnet hosts using threading in python, but I stumbled about an issue I’m not able to solve.

Using the following code on MAC OS X Lion / Python 2.7

import threading,telnetlib,socket

class ReaderThread(threading.Thread):
    def __init__(self, ip, port): 
        threading.Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.telnet_con = telnetlib.Telnet()

    def run(self):
        try:
            print 'Start %s' % self.ip
            self.telnet_con.open(self.ip,self.port,30)
            print 'Done %s' % self.ip
        except socket.timeout:
            print 'Timeout in %s' % self.ip

    def join(self):
        self.telnet_con.close()

ta = []

t1 = ReaderThread('10.0.1.162',9999)
ta.append(t1)
t2 = ReaderThread('10.0.1.163',9999)
ta.append(t2)

for t in ta:
    t.start()
print 'Threads started\n'

In general it works, but either one of the threads (it is not always the same one) takes a long time to connect (about 20 second and sometimes even runs into a timeout). During that awfully long connection time (in an all local network), cpu load also goes up to 100 %.

Even more strange is the fact that if I’m using only one thread in the array it always works flawlessly. So it must have something to do with the use of multiple threads.

I already added hostname entries for all IP addresses to avoid a DNS lookup issue. This didn’t make a difference.

Thanks in advance for your help.

Best regards

senexi

  • 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-27T18:10:21+00:00Added an answer on May 27, 2026 at 6:10 pm

    Ok, You have overridden join(), and you are not supposed to do that. The main thread calls join() on each thread when the main thread finishes, which is right after the last line in your code. Since your join() method returns before your telnet thread actually exits, Python gets confused and tries to call join() again, and this is what causes the 100% cpu usage. Try to put a ‘print’ statement in your join() method.

    Your implementation of join() tries to close the socket (probably while the other thread is still trying to open a connection), and this might be what causing your telnet threads to never finish.

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

Sidebar

Related Questions

I want to write an application which should be able to connect to multiple
I would be using a USB hub to connect multiple devices. I want to
I want to use multiple private keys to connect to different servers or different
I want to connect and execute one (or sometimes several) SQL statements, and NOT
I want to connect to DB using the iSeries Client Access driver. I use
I want to connect from home using SQL Server 2005 to another PC. I
I just installed VS2010 and I want to connect to TFS, But I don't
I want to connect to multiple TFS server from Visual Studio 2010 team explorer.
I'm using boost::signals and leaking memory when I try to connect multiple signals to
I want to be able to host multiple NodeJS apps under the same domain,

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.