[FAQTS] Python Knowledge Base Update -- July 27th, 2000

Fiona Czuczman fiona at sitegnome.com
Thu Jul 27 06:53:32 EDT 2000


Hi Guys,

Here are the latest entries into http://python.faqts.com

enjoy,

Fiona

Including:

- How can I determine the number of threads that are running?
- Searching an example that has multiple threads write to the Queue 
object?
- How to build arbitrarily-sized lists in one fell swoop?
- Does anybody know of a compiler for python?


## New Entries #################################################


-------------------------------------------------------------
How can I determine the number of threads that are running?
http://www.faqts.com/knowledge-base/view.phtml/aid/5043
-------------------------------------------------------------
Fiona Czuczman
Aahz Maruch

If you are using threading.py, and if you create all your threads using
threading.Thread() (or subclasses thereof), then you can use
threading.activeCount().  However, you should be aware of two caveats:

* The main thread that starts up the subthreads *does* count as a
thread, so your thread count is always one higher than your first guess.

* While occasionally useful for what I call "brute force" threading,
activeCount() is mostly useless in "real" threaded programs.  If you
really want to know how many threads are running, use some kind of
variable that you increment and decrement through a mutex.  But there
are usually no reasons other than debugging to do this.


-------------------------------------------------------------
Searching an example that has multiple threads write to the Queue object?
http://www.faqts.com/knowledge-base/view.phtml/aid/5044
-------------------------------------------------------------
Fiona Czuczman
Robert W. Bill

If you hypothetically wanted to have one class log events from threads,
you could subclass threading.Thread and start with something that looks
like this:

from threading import *
from Queue import Queue

class clientThread(Thread):
    def __init__(self, q, myNumber):
        self.q = q
        self.myNumber = myNumber
        Thread.__init__(self)
                        
    def run(self):
        # do something wacky #
        logline = "This is a log from thread #" + str(self.myNumber)
        self.q.put(logline) # this blocks--see Python Library Ref
                            # you may want to wrap in try/except
                            # with put_nowait

class logger(Thread):
    def __init__(self, q):
        self.q = q
        Thread.__init__(self)
        
    def run(self):
        print "logger started"
        # replace next line (range) with an event or something more 
        # useful
        for i in range(10):
            logEntry = q.get() # this blocks--see Python Library Ref
                               # you may want get_nowait() in try/except
            print logEntry

if __name__=='__main__':
    q = Queue(10)
    logger(q).start()
    for i in range(10):
        clientThread(q,i).start()


-------------------------------------------------------------
How to build arbitrarily-sized lists in one fell swoop?
http://www.faqts.com/knowledge-base/view.phtml/aid/5045
-------------------------------------------------------------
Fiona Czuczman
Remco Gerlich

l = [x]*n

Does what you do here. I don't know what kind of object x is, but note
that this doesn't make *copies* of x; if it's some instance, then all
elements of l will be a reference to the same old x. Same for your
methods.


-------------------------------------------------------------
Does anybody know of a compiler for python?
http://www.faqts.com/knowledge-base/view.phtml/aid/5046
-------------------------------------------------------------
Fiona Czuczman
Shae Matijs Erisson

Jeremy Hylton has a source to bytecode compiler written in Python.
it's in the python/nondist/Compiler/ part of the CVS tree.

Greg Stein and Bill Tutt wrote a Python2C translator, which cuts at
least 10percent off of running time from what I've read. The C file
that's written is a Python module, and so won't let you make a pure
executable though.
It's at http://lima.mudlib.org/~rassilon/py2c/ (or was last I checked)

A python compiler is definitely possible, especially once you've checked
out the two projects mentioned above. I don't know how much it's needed
though.







More information about the Python-list mailing list