Results 1 to 3 of 3

Thread: Ice-python looses threadlocals

  1. #1
    filip84 is offline Registered User
    Name: Filip Paczynski
    Organization: Personal
    Project: No real project, testing ice with python
    Join Date
    Jan 2011
    Posts
    7

    Ice-python looses threadlocals

    Hi,
    it seems that threadlocals (instances of python's threading.local()) are not preserved in between calls from Ice runtime to python runtime for the same thread.

    Simple example based on printer demo:
    Code:
    import sys, traceback, Ice
    import threading
    from pprint import pprint, pformat
    
    #Ice.loadSlice('Printer.ice')
    #import Demo
    
    #class PrinterI(Demo.Printer):
    #    def printString(self, s, current=None):
    #        print s
    
    class ThreadLogger(Ice.ThreadNotification):
    
        def __init__(self):
            super(ThreadLogger, self).__init__()
            self._tls = threading.local()
    
        def start(self):
            print "ice thread started: " + repr(threading.current_thread()) + " thread locals:"
            self._tls.my_test_var = "This won't survive until stop()"
            pprint(self._tls.__dict__)
    
        def stop(self):
            print "ice thread stopped: " + repr(threading.current_thread()) + " thread locals:"
            pprint(self._tls.__dict__)
        
    
        
    
    status = 0
    ice = None
    try:
        initData = Ice.InitializationData()
        initData.threadHook = ThreadLogger()
        ic = Ice.initialize(sys.argv, initData)
        adapter = ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000")
        #object = PrinterI()
        #adapter.add(object, ic.stringToIdentity("SimplePrinter"))
        adapter.activate()
        ic.waitForShutdown()
    except:
        traceback.print_exc()
        status = 1
    
    if ic:
        # Clean up
        try:
            ic.destroy()
        except:
            traceback.print_exec()
            status = 1
    
    sys.exit(status)
    To reproduce the bug please run the above script - it should print that it has set up a threadlocal var for two ice threads. Next, please interrupt the script, which will cause ice runtime to call the threadHook.stop() and method shut down the threads that it has started. The threadlocal value set up in threadHook.start() is no longer present when calling threadHook.stop().

    Using threadHook is the simplest and shortest way to demonstrate this behaviour, but is also occurs for example in servant locators - in this case threadlocals are not preserved between calls to locate() and finished().

    Tested on win xp 32bit:
    - Ice 3.4.1 official binary downloaded from the site
    - Python 2.6.6 32-bit official binary

    and mac os x 10.6.7:
    - Ice 3.4.1 from MacPorts
    - Python 2.6.6 from MacPorts

  2. #2
    mes's Avatar
    mes
    mes is offline ZeroC Staff
    Name: Mark Spruiell
    Organization: ZeroC, Inc.
    Project: Ice Developer
    Join Date
    Feb 2003
    Location
    California
    Posts
    1,441
    Filip,

    Thanks for reporting this.

    Cheers,
    Mark

  3. #3
    filip84 is offline Registered User
    Name: Filip Paczynski
    Organization: Personal
    Project: No real project, testing ice with python
    Join Date
    Jan 2011
    Posts
    7
    It seems, that the problem occurs only when python is using thread._local as the implementation of threading.local. I guess thread._local is more optimized and os-specyfic. However, if I force python to use the default (and probably much slower) implementation, threadlocals are not lost:

    Code:
    import threading
    import _threading_local
    threading.local = _threading_local.local
    
    
    import sys, traceback, Ice
    from pprint import pprint, pformat
    
    class ThreadLogger(Ice.ThreadNotification):
        def __init__(self):
            super(ThreadLogger, self).__init__()
            self._tls = threading.local()
    
        def start(self):
            self._tls.my_test_var = "_threading_local.local works"
            print "ice thread started: " + repr(threading.current_thread()) + " thread locals: " + pformat(self._tls.__dict__)
    
        def stop(self):
            print "ice thread stopped: " + repr(threading.current_thread()) + " thread locals: " + pformat(self._tls.__dict__)
        
        
    status = 0
    ice = None
    try:
        initData = Ice.InitializationData()
        initData.threadHook = ThreadLogger()
        ic = Ice.initialize(sys.argv, initData)
        adapter = ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000")
        adapter.activate()
        ic.waitForShutdown()
    except:
        traceback.print_exc()
        status = 1
    
    if ic:
        # Clean up
        try:
            ic.destroy()
        except:
            traceback.print_exec()
            status = 1
    
    sys.exit(status)
    I wonder if this bug is strictly ice-specific, or is python also involved?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Ice for Python + VS2008?
    By pchapin in forum Help Center
    Replies: 2
    Last Post: 03-16-2009, 11:25 PM
  2. Dynamic Ice for Python
    By Edward Bishop in forum Help Center
    Replies: 3
    Last Post: 05-03-2007, 07:21 PM
  3. C++, Java and Python Ice in now in MacPorts
    By blair in forum Comments
    Replies: 0
    Last Post: 04-10-2007, 01:25 AM
  4. Ice for Python
    By davidcr1983 in forum Help Center
    Replies: 3
    Last Post: 07-27-2005, 12:01 PM
  5. Ice Extension for Python
    By shantanu_k06 in forum Comments
    Replies: 1
    Last Post: 11-08-2003, 12:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •