2

I am trying to continuously through time from client side to server side even if server is not accepting the connection I have written following function on client side

def run(self):

    print "running client"  
    start = datetime.now().second
    while True:
        try:
            host ='localhost'
            port = 5010
            time = abs(datetime.now().second-start)
            time = str(time)
            print time
            client = socket.socket()
            client.connect((host,port))
            client.send(time)

        except socket.error:
            pass 

Now I am inside another class its name is loginGui, and i want to execute above global function run with the help of the a thread so that client continuously throws the time to server side while my rest of client code executes. So i wrote following code inside a class loginGui

class loginGui:
    def run_client(self):
            thread.start_new_thread(run,())

when I call the above function run_client(), thread runs but for some reasons it comes out of the loop and stops throwing the time so I tried this another approach..

class FuncThread(threading.Thread):

    def __init__(self, target, *args):
        self._target = target
        self._args = args
        threading.Thread.__init__(self)

    def run(self):
        self._target(*self._args)

I wrote above class and then in run_client() function I did following changes..

def run_client(self):

    t1 = FuncThread(run,())
    t1.start()
    t1.join()

Now when I call run_client() it creates a thread and calls the global function run() but now the problem is it get struck in the while loop and keep throwing the time.

conclusion: What I basically want is to create a thread which call my function run and keep throwing the time without getting struck in while loop..

1 Answer 1

3

In your first example, your while loop is firing at an extremely fast unchecked rate, creating connections over and over again that aren't closed. You need to try and put some type of limit on there, be it a sleep, or something like that. Try reusing the same client instead of throwing them away. Its possible you might be hitting some other type of exception, like running out of resources eventually. You only need to attempt to create a new client if you failed to create it the previous time. Not just every time you want to send another bit of data.

In your second example, you are doing pretty much the same thing as before, except moving the code into the actual thread class, but then you call join() which is going to block in the main thread until your thread is complete.

In a nutshell, I don't believe the issue is where you are constructing this thread class, but rather how you are using the socket connections in a wasteful manner.

This example is kind of ugly, but I think it will give you a jumping off point:

import time

def run(self):

    print "running client"  
    start = datetime.now().second
    host ='localhost'
    port = 5010

    client = None

    while True:

        if client is None:
            client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                client.connect((host,port))
            except socket.error:
                print "Connection failed. Retrying."
                client = None
                time.sleep(.25)
                continue


        time = str(abs(datetime.now().second-start))
        print time
        try:
            client.sendall(time)
        except:
            print "Send error."
            client.close()
            client = None

        time.sleep(.25)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.