【问题标题】:Not able to lock the thread in socket using Multiprocess无法使用多进程锁定套接字中的线程
【发布时间】:2019-11-19 01:54:27
【问题描述】:

我已经浏览了链接:multiprocessing.Pool - PicklingError: Can't pickle <type 'thread.lock'>: attribute lookup thread.lock failed
我仍然没有得到解决方案。
这是我尝试过的:
服务器

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")

这是客户:

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()

最后收到错误:

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

请。让我知道这种情况的解决方案。我正在使用Python 3.5.0

【问题讨论】:

  • 你到底想做什么?
  • 我正在尝试处理多个请求并且他们到达并想要为他们服务。我正在尝试使用 api 在 iqoption 服务器上应用出价。但这不起作用,正如您看到的那样,我遇到了错误。
  • 对。我认为 IQOption API 对象拥有一个线程,该线程不能传递给子进程。改用线程。
  • @AKX ^这可能应该在您的答案中,因此未来的读者不必深入研究 cmets...
  • @Aaron 好点。完成。

标签: python python-3.x sockets multiprocessing


【解决方案1】:

IQOption API 对象可能拥有一个线程,该线程不能传递给子进程。改为使用线程进行并发...

这是使用内置socketserver 模块的服务器代码的大致等效(未经测试)版本,使用threads (with ThreadingMixIn) 并行化每个客户端连接。

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()

【讨论】:

  • 抱歉,我不能使用 UDP 服务器。服务器必须是 TCP。我不能丢失数据。
  • @JafferWilson 然后只需替换 socketserver.TCPserver... 链接的文档有许多各种配置的示例。
  • 我的错。我已将 UDP 复制粘贴到示例中。
  • 需要检查。请允许我试一试。
  • 我收到AttributeError: __exit__,请您帮忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-18
  • 2014-03-14
  • 2011-03-18
  • 2020-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多