Jaffer Wilson
Jaffer Wilson

Reputation: 7273

Not able to lock the thread in socket using Multiprocess

I have gone through the link: multiprocessing.Pool - PicklingError: Can't pickle <type 'thread.lock'>: attribute lookup thread.lock failed
Still I did not got the solution for that.
Here is what I have tried:
Server

import multiprocessing
import socket
from iqoptionapi.stable_api import IQ_Option
import time

def handle(client_socket,address,I_want_money):
    print(address)
    client_socket.sendall("Happy".encode())
    client_socket.close() 
    return
class Server(object):
    def __init__(self, hostname, port):
        # import logging
        # self.logger = logging.getLogger("server")
        self.hostname = hostname
        self.port = port
        self.I_want_money=IQ_Option("email","password")
        self.I_want_money.suspend = 0.1
        print("I am ON.........")


    def start(self):
        # self.logger.debug("listening")
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.bind((self.hostname, self.port))
        self.socket.listen(1)

        while True:
            conn, address = self.socket.accept()
            # self.logger.debug("Got connection")
            process = multiprocessing.Process(target=handle, args=(conn, address,self.I_want_money,))
            process.daemon = True
            process.start()
            # self.logger.debug("Started process %r", process)

if __name__ == "__main__":
    # import logging
    # logging.basicConfig(level=logging.DEBUG)

    server = Server("0.0.0.0", 9000)
    try:
        # logging.info("Listening")
        server.start()
    except Exception as e:
        print(e, "\n I had experienced at initialization")
        # logging.exception("Unexpected exception")
    finally:
        # logging.info("Shutting down")
        for process in multiprocessing.active_children():
            # logging.info("Shutting down process %r", process)
            process.terminate()
            process.join()
    # logging.info("All done")

This is the client:

import socket
if __name__ == "__main__":
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(("localhost", 9000))
    for i in range(100):        
        data = "some data"
        sock.sendall(data.encode())
        result = sock.recv(1024)
        print(result)
    sock.close()

And finally the error received:

login...
I am ON.........
Can't pickle <class '_thread.lock'>: attribute lookup lock on _thread failed
 I had experienced at initialization
Press any key to continue . . . Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python35\lib\multiprocessing\spawn.py", line 100, in spawn_main
    new_handle = steal_handle(parent_pid, pipe_handle)
  File "C:\Python35\lib\multiprocessing\reduction.py", line 81, in steal_handle
    _winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87] The parameter is incorrect

Kindly. let me know the solution for this sort of situation. I am using Python 3.5.0

Upvotes: 0

Views: 317

Answers (1)

AKX
AKX

Reputation: 169124

The IQOption API object probably owns a thread, which can't be passed through to subprocesses. Use threads for concurrency instead...

Here's a roughly equivalent (untested) version of your server code using the built-in socketserver module, using threads (with ThreadingMixIn) to parallelize each client connection.

from iqoptionapi.stable_api import IQ_Option
import socketserver

I_want_money = IQ_Option("email", "password")
I_want_money.suspend = 0.1


class RequestHandler(socketserver.BaseRequestHandler):
    def handle(self):
        print(self.client_address)
        request.sendall("Happy".encode())
        request.close()


server = socketserver.ThreadingTCPServer(("0.0.0.0", 9000), RequestHandler)
server.serve_forever()

Upvotes: 2

Related Questions