views:

234

answers:

2

I have never programed in Python before, so excuse my code. I have this script that will run in a terminal but I can't get it to run client side. I am running this in Appcelerator's Titanium application. Anyway, I have been troubleshooting it and it seems that it isn't running the threads at all. Is this a limitation? does anyone know?

<script type="text/python">
import os
import sys
import Queue
import threading
class FindThread ( threading.Thread ):
   def run ( self ):
      running = True
      while running:
         if jobPool.empty(): 
            #print '<< CLOSING THREAD'
            running = False
            continue

         job = jobPool.get()
         window.document.getElementById('output').innerHTML +=  os.path.join(top, name)
         if job != None:
            dirSearch(job)             

jobPool = Queue.Queue ( 0 )

def findPython():
    #output = window.document.getElementById('output')
    window.document.getElementById('output').innerHTML += "Starting"
    dirSearch("/")
    # Start 10 threads:
    for x in xrange ( 10 ):
        #print '>> OPENING THREAD'
        FindThread().start()

def dirSearch(top = "."):
    import os, stat, types
    names = os.listdir(top)
    for name in names:
        try:
            st = os.lstat(os.path.join(top, name))
        except os.error:
            continue
        if stat.S_ISDIR(st.st_mode):
            jobPool.put( os.path.join(top, name) )
        else:
            window.document.getElementById('output').innerHTML +=  os.path.join(top, name)

window.findPython = findPython

</script>
+1  A: 

Whoa, is that Python doing DOM manipulation in a web browser? Typically, a web browser doesn't let you spawn threads, so I'd chalk this up to a limitation of the platform.

Jacob
this is using "appcelerator"... so it technically is a web page, but it is running in an desktop application
Phobis
+1  A: 

The answer, currently (Friday, June 19th, 2009) is yes, it can run threads, but the nothing but the main thread can access JavaScript objects, this includes the DOM. so if you are planning on updating the UI with a threading app, this is not possible... YET. Until the Appcelerator team creates some sort of queue to the main thread, possible via a binding system.

Please see discussion at the appcelerator forums.

Phobis