0

Im not quite sure what to classify this problem as, but as far as i can tell the connhandler thread should be able to write to the dictionary stored in the queuemanager class that is defined in hostmain, however when i print it out from the terminal prompt it comes up as empty, and when the thread and hostmain print the class object they have different memory addresses. This is my first project with multithreading and socketserver so i may have done something wrong, but to my knowledge i cant see why the thread from socketserver is unable to access the same servermanager object from hostmain.

To clarify a bit this project is using the socketserver library with the threading mixin, and the the threading library for everything showed in the example.

Request handler class code as well as a pastebin with pared down versions of pertinent code, reproducing the issue: https://pastebin.com/u/kadytoast/1/PPWfyCFT

import testhostmain as hmain
import multiprocessing as mp

def joinpacket(flag, data):
    return f"{flag}{hmain.packetdelim}{data}"

def splitpacket(msg):
    msg = msg.split(hmain.packetdelim)
    flag, data = msg[0], msg[1]
    return flag, data

class ConnHandler(soss.BaseRequestHandler):
    def handle(self):
        # request handler to spawn processes
        print(hmain.servermanager, "in connhandler")
        
        # collecting peritype and name
        msg = self.request.recv(hmain.buffer).decode("utf-8")
        flag, data = splitpacket(msg)

        data = data.split(hmain.datadelim)
        self.peritype, self.macid = data[0], data[1]
        #print(data)

        # checks the header flag
        if flag == hmain.newconflag:
            hmain.servermanager.procdict[self.macid] = "connected"
            print(hmain.servermanager.procdict, "from connhandler")

screenshot of the terminal showing the issue

in this image you can see the two class printouts with their memory addresses, i would assume they should be the same but im not sure on that, however the dictionary printout from connhandler is correct but doesnt show up from the current process dictionary printout.

is there some conflict trying to use the threading mixin as well as seperately called threads?

Thankyou for getting this far if you did, if i need to provide anymore information i am happy to oblige. Thankyou again!

Edit: i expect i may have to add usage of the threading.lock, but nothing is writing to the dictionary at all so i dont expect that to be the issue in this case

kadytoast
  • 33
  • 4
  • I don't know, but I'd imagine there are lots of proxy objects used in `multiprocessing`. – Peter Wood Aug 23 '21 at 23:06
  • @PeterWood sorry would you be able to elaborate a little with that? this is my first project using concurrency libraries, and if i understand the threading library i shouldnt need to use proxies as the memory space should be shared right? – kadytoast Aug 24 '21 at 01:46
  • It's an implementation detail. Printing the `QueueManager` shows just the memory location of the Python object which represents the `QueueManager`. If there is any shared memory it isn't showing you that. It's just the default representation of a Python object, not meant to inform you about the inner workings. – Peter Wood Aug 24 '21 at 11:27

0 Answers0