0

I have so been working on something for the past few days, and i now am in the final steps which is adding multiprocessing or multithreading. After seeing that pickling SSLSocket objects in multiprocessing is not easy, i decided to go with multithreading ( i chose this also because its for making web requests which is I/O). I added the threading part to my code, and if i only start one single thread, it works fine, but after adding 2 threads, it starts throwing errors at me that i have never seen before.

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = ssl.wrap_socket(s, keyfile=None, certfile=None, server_side=False, cert_reqs=ssl.CERT_NONE,          ssl_version=ssl.PROTOCOL_SSLv23)
t1 = threading.Thread(target=check, args=(s,))
t1.start()
t2 = threading.Thread(target=check, args=(s,))
t2.start()

this is in the if name_== main portion of my code. I put this here so when i called my other functions i could pass the socket into the function and reuse the connection. Here is my function:

def check(socket):
    for x in range(5):
        uid_data = []
        socket.settimeout(.5)
        socket.send()
        while True:
            try:
                response = socket.recv(4094)
                uid_data.append(response)
            except Exception as e:
                break

let me start of by saying that this code works perfectly without threading/processes. So i know its not my code. I dont really know whats going on because it works for around 3-4 attempts then itll error. Here is the traceback:

    return self._sslobj.write(data)
    OSErrorreturn self._sslobj.write(data):
    [Errno 0] ErrorOSError
    : [Errno 0] Error

This is from the line socket.send(), (its what the traceback says). Why is it doing this when i try to run multiple threads?

coke
  • 7
  • 4
  • I think that socket.send(..) is not thread-safe. Check this [post](https://stackoverflow.com/questions/11177018) – JohnRC Apr 30 '20 at 14:55
  • yeah, i added locks to the socket.send function, but it just made it 10x slower. – coke Apr 30 '20 at 23:41

0 Answers0