2

I am trying to create a separate thread in a client that revives messages from a server socket in a non-blocking manner. Since my original code is too long and a bit of a hassle to explain in order to understand it, I have created an example program which focuses on what I want to do. I try to create two separate threads , say Thread t1 and Thread t2. Thread t1 polls the socket to check for any received data whereas Thread t2 does whatever task it is assigned to do. What I am expecting it to do is, Thread t1 always polls and if a data is received it prints it on the screen and Thread t2 executes in parallel doing whatever it is doing. But, I cannot get it working for some reason.

My example program is:

import threading
import time
import threading
from time import sleep
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 5555))
s.setblocking(0)
s.sendall(str.encode('Initial Hello'))

def this_thing():
    while True:
        try:
           data = s.recv(4096)
            print(data.decode('utf-8'))
        except:
            pass / break #not sure which one to use. Neither of it works

def that_thing():
    for i in range(10000):
        sleep(3)
        s.sendall(str.encode('Hello')
        print('happening2')

threading.Thread(target=this_thing(), args=[]).start()
threading.Thread(target=that_thing(), args=[]).start()

Note: The server socket is a simple server that sends a message to all connected sockets if a message was received by it. When I run the program by breaking out in the exception in Thread t1, only my Thread t2 is keeps running. I.e Thread t1 does not receive any data sent from the server

7
  • you shouldn't ever pass in an except block, catch the exception and log it or just print it. To catch any exception you can just do, try: code() except Exception as e: print(e). The more specific the exception the better though. Commented Apr 25, 2017 at 15:15
  • @chatton : The exception is: [WinError 10035] A non-blocking socket operation could not be completed immediately Which makes sense, but if i change the socket to blocking it never really gets out of Thread t1 until I receive a message which is not what I want to do. Any way around this? Commented Apr 25, 2017 at 15:18
  • threading.Thread(target=this_thing(), args=[]).start(), I'm pretty sure the target is supposed to be the function itself, and not the result of the function call. target=this_thing() -> target=this_thing I think what's happening now is that during creation of the first thread it gets stuck in an infinite loop. Commented Apr 25, 2017 at 15:27
  • from the docs "target is the callable object to be invoked by the run() method" docs.python.org/2/library/threading.html in this case, this_thing and that_thing are the callable objects. Commented Apr 25, 2017 at 15:31
  • 1
    you should be able to simply change target=this_thing() to target=this_thing and the same for that_thing Commented Apr 25, 2017 at 15:36

2 Answers 2

3

The reason this is happening is because the "target" argument takes a callable object.

from the docs docs.python.org/2/library/threading.html

"target is the callable object to be invoked by the run() method"

in your version

threading.Thread(target=this_thing(), args=[]).start()
threading.Thread(target=that_thing(), args=[]).start()

when you say target=this_thing(), it will try and evaluate the value of a call to this_thing, in your case, it will enter a while True loop, and then if it was to finish it would evaluate to None.

What you want to do is replace these 2 lines with

threading.Thread(target=this_thing, args=[]).start()
threading.Thread(target=that_thing, args=[]).start()

Note that you are now passing in the function itself. A function is a callable object.

Sign up to request clarification or add additional context in comments.

Comments

0

The correct solution for python 3+ isn't multithreading but asyncio.

Check out this awesome speech from David Beazley on the matter (49 mins): https://www.youtube.com/watch?v=ZzfHjytDceU

Asyncio / sockets example: https://gist.github.com/gregvish/7665915

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.