Threading in nautilus-python extensions

View: New views
2 Messages — Rating Filter:   Alert me  

Threading in nautilus-python extensions

by J Heeris :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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. Drop it in ~/.nautilus/python-extensions
and use "nautilus -q && nautilus --no-desktop" to kill and restart
nautilus and log to the console.

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 asked a
few days ago on the Nautilus Dev list[2], and I was directed here.

Cheers,
Jason Heeris

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

[2] http://mail.gnome.org/archives/nautilus-list/2009-July/msg00000.html

PS. I think a 48 hour moderation queue might just be a little on the
extreme side... :)


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



_______________________________________________
pygtk mailing list   pygtk@...
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

signature.asc (852 bytes) Download Attachment

Re: Threading in nautilus-python extensions

by J Heeris :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Just to follow up on this a bit, it really seems to be something to do
with the nautilus-python extension specifically. One of the other
NautilusSVN developers found that an extension written in C does not
have the same problem at all:

----
I've created a simple nautilus extension written in C that creates a
thread and sleeps in it, and it doesn't block nautilus at all.  I've
got the code up at github at
git://github.com/adamplumb/nautilus-test-extension.git.  Just do a git
clone to get it.  Then compile/install with sh autogen.sh ; make ;
make install.  (FYI It will install files to
/usr/lib/nautilus/extensions-2.0).
----

But is there a way to work around this without using C? I'm poring
over the nautilus-python and pygtk source, but I'm not really seeing
anything enlightening.

Thanks,
Jason
_______________________________________________
pygtk mailing list   pygtk@...
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/