Threading in nautilus-pytohn extensions



Hi,

I'm working on NautilusSVN, a Python-based extension to integrate
Subversion into Nautilus. I've run into some confusing issues regarding
threading and I was hoping for some advice.

Basically, what I've found is that threads spawned by the Python
extension do not run unless the user specifically interacts with
Nautilus (eg. refreshes, selects files, opens context menu).

I've attached some code - it's the smallest possible example (I think)
that illustrates the problem.

What I would expect is that the test extension logs once every 1/100th
of a second. What actually happens is that it only logs when I interact
with Nautilus.

So what exactly is happening here? Is it expected?

I would appreciate any advice on this.

Just some background: one of the previous maintainers for this project
had the same problems[1], but I couldn't find any resolution. (I'd like
to point out that searching the mailing list archives for anything
involving threading seems nearly impossible, since I just get the thread
indexes of the list itself...)

Cheers,
Jason Heeris

[1] http://mail.gnome.org/archives/nautilus-list/2006-December/msg00053.html
'''
AsyncTest.py

Created on 27/06/2009

This is an attempt to create the simplest possible test for threading in a
Python extension for Nautilus.

@author: Jason Heeris
'''

import nautilus
import gobject

import threading
import time

import logging
        
class AsyncTest(nautilus.MenuProvider):
    ''' Simple test class for multithreaded Python Nautilus extension.
    '''
    
    def __init__(self):
        logging.getLogger().setLevel(logging.DEBUG)
    
    def get_background_items(self, window, files):
        '''
        Gets the context menu entry.
        '''
        menu_item = nautilus.MenuItem(
            'AsyncTest',
            'Test Async. Behaviour',
            'Tests multithreading in python-nautilus extensions'
        )
        
        menu_item.connect('activate', self.test_asynchronicity)
        
        return [menu_item]
        
    def test_asynchronicity(self, *args, **kwargs):
        '''
        This is a function to test doing things asynchronously.
        '''
              
        def asynchronous_function():
            
            logging.getLogger().setLevel(logging.DEBUG)

            logging.debug('\n%s Inside asynchronous_function()' % time.time())         
            
            for i in range(1, 21):
                time.sleep(0.01)
                logging.debug('%s %0i Asynchronous thread still running...' % (time.time(), i))
                logging.debug('Current thread: %s' % threading.currentThread())
                logging.debug('Is demon: %s' % threading.currentThread().isDaemon())
            
            logging.debug("%s asynchronous_function() finished\n" % time.time())
        
        # Calling threads_init does not seem to do anything.
        logging.debug('Current thread: %s' % threading.currentThread())
        gobject.threads_init()
        threading.Thread(target=asynchronous_function, name='Async Test').start()

Attachment: signature.asc
Description: OpenPGP digital signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]