为什么在Python中,threading.Thread向线程函数传递空字典?

0 投票
1 回答
613 浏览
提问于 2025-04-18 01:28

我想在创建线程的时候,把一个字典传给它。下面是我代码中和这个问题相关的部分(这段代码试图把一个websocket映射到另一个非websocket):

class SimpleEcho(WebSocket):
     def SockFun(self,**FlagDict):
         try:    
             print "6"
             ms=mysocket()
             print "7"
             ms.connect("127.0.0.1",7778)
             print "8"
             while (1):
                 print FlagDict
                 print "9"
                 FlagDict["sockexists"]=1
                 print "90"
                 #data=FlagDict["Data"]
                 msg=str(self.data)
                 self.data=''
                 print "91"
                 #FlagDict["Data"]=''
                 print "92"
                 #msg=str(self.data)
                 print msg
                 print "93"
                 #print self.FlagDict
                 ms.mysend(msg)
                 print "10"
                 a=ms.myreceive()
                 print "11"
                 self.sendMessage(a)
                 print "12"
             FlagDict["sockexists"]=0
             print "13"
             FlagDict["threadexists"]=0
             ms.myclose()
             print "14"
         except Exception as a:
             FlagDict["sockexists"]=0
             FlagDict["threadexists"]=0
             ms.myclose()
             print "error in SockFun",a


     def handleMessage(self):
         print "2"
         if self.data is None:
             print "3"
             self.data = ''
         else:
             self.FlagDict["Data"]=self.data
             self.CheckThread()
             print "20"

class WebSocket(threading.Thread):
     def __init__(self, server, sock, address):
         self.server = server
         self.client = sock
         self.address = address

         self.handshaked = False
         self.headerbuffer = ''
         self.readdraftkey = False
         self.draftkey = ''
         self.headertoread = 2048
         self.hixie76 = False

         self.fin = 0
         self.data = None
         self.opcode = 0
         self.hasmask = 0
         self.maskarray = None
         self.length = 0
         self.lengtharray = None
         self.index = 0
         self.request = None
         self.usingssl = False

         self.state = self.HEADERB1

         # restrict the size of header and payload for security reasons
         self.maxheader = 65536
         self.maxpayload = 4194304
         self.FlagDict={}
         self.FlagDict["sockexists"]=0
         self.FlagDict["threadexists"]=0
     def CheckThread(self):
             self.FlagDict["Data"]=self.data
             print self.FlagDict
             try:
                 print "4" 
                 if self.FlagDict["threadexists"]==0:
                     print "5"
                     self.FlagDict["threadexists"]=1
                     print "15"
                     ts=threading.Thread(target=self.SockFun(),args=self.FlagDict)
 #                    ts=threading.Thread(target=self.SockFun())
                     print "16"
                     ts.deamon=True
                     print "17"
                     ts.start
                     print "18"
                 print "19"
             except Exception as d:
                 print "error in handleMessage",d
                 print "202"

运行时的代码输出如下:

('127.0.0.1', 51180) 连接成功 1 2 {'Data': bytearray(b'Page-1;'), 'sockexists': 0, 'threadexists': 0} 4 5 15 6 7 8 9 {} 90 91 92 Page-1; 93 10 11 12 {'sockexists': 1} 9 90 91 92

93 10 11 12 {'sockexists': 1} 9 90 91 92

93 10 11 12 {'sockexists': 1} 9 90 91 92

93 10 11 12 {'sockexists': 1} 9 90 91 92

93 10 11 12 {'sockexists': 1} 9 90 91 92

93 10

从输出中可以看到,线程在调用SockFun函数时传递了一个空字典。为什么会这样呢?我该如何解决这个问题呢?如果想了解更多关于WebSocket模块的信息,可以查看以下链接:

https://github.com/opiate/SimpleWebSocketServer http://opiate.github.io/SimpleWebSocketServer/

1 个回答

1

好的,我不会用你的代码,只给你一个例子,相信你自己能处理好:

>>> from threading import Thread
>>> def f(**kwargs):
        print(kwargs)

>>> Thread(target=f, kwargs={'1':1, '2':2}).start()
{'2': 2, '1': 1}

撰写回答