Will multithreading make python less popular?

Michele Simionato michele.simionato at gmail.com
Mon Feb 16 23:53:39 EST 2009


On Feb 16, 10:20 pm, rushen... at gmail.com wrote:
> Hi again
>
> OpenERP and ERP5 was written in python as i know. I really wonder how
> they do this without threads. I want to see a real time graph at the
> same time while i am working on the same screen. What is the secret?
>
> Thanks
> Rushen

Here is an example of using multiprocessing (which is included
in Python 2.6 and easy_installable in older Python versions)
to print a spin bar while a computation is running:

import sys, time
import multiprocessing

DELAY = 0.1
DISPLAY = [ '|', '/', '-', '\\' ]

def spinner_func(before='', after=''):
    write, flush = sys.stdout.write, sys.stdout.flush
    pos = -1
    while True:
        pos = (pos + 1) % len(DISPLAY)
        msg = before + DISPLAY[pos] + after
        write(msg); flush()
        write('\x08' * len(msg))
        time.sleep(DELAY)

def long_computation():
    # emulate a long computation
    time.sleep(3)

if __name__ == '__main__':
    spinner = multiprocessing.Process(
        None, spinner_func, args=('Please wait ... ', ''))
    spinner.start()
    try:
        long_computation()
        print 'Computation done'
    finally:
        spinner.terminate()



More information about the Python-list mailing list