ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: z_sync.py
Submitter: Stephen Chappell (other recipes)
Last Updated: 2006/07/06
Version no: 1.0
Category: OOP

 

Not Rated yet


Description:

This recipe gives access to the Sync class.
Objects created by the Sync class are meant to
be used when trying to syncronize the execution
of two or more threads. See z_service.py for more
a simple example of how Sync objects can be used.

Source: Text Source

'''Support module for syncronizing threads.

This module allows access to the Sync class which can
allow automatic sycronization across several threads.'''

__version__ = 1.1

################################################################################

import thread

class Sync:

    'Sync(threads) -> new syncronizer object'

    def __init__(self, threads):
        'x.__init__(...) initializes x'
        self.__threads = threads
        self.__count = 0
        self.__main = thread.allocate_lock()
        self.__exit = thread.allocate_lock()
        self.__exit.acquire()

    def sync(self):
        'Automatically syncronizes threads.'
        self.__main.acquire()
        self.__count += 1
        if self.__count < self.__threads:
            self.__main.release()
        else:
            self.__exit.release()
        self.__exit.acquire()
        self.__count -= 1
        if self.__count > 0:
            self.__exit.release()
        else:
            self.__main.release()

################################################################################

if __name__ == '__main__':
    import sys
    print 'Content-Type: text/plain'
    print
    print file(sys.argv[0]).read()

Discussion:

The following code gives one example of how Sync objects can be used.

import os
import random
import sync
import time

SLOTS = 10

def reader(slots):
    while True:
        if os.name == 'nt':
            os.system('cls')
        elif os.name == 'posix':
            os.system('clear')
        print str(slots)[1:-1]

def writer(slots, index, sleep):
    while True:
        if not random.randrange(100000):
            sleep.sync()
            time.sleep(5)
        slots[index] = (slots[index] + 1) % 10

def main():
    slots = [0] * SLOTS
    sleep = sync.Sync(SLOTS)
    sync.thread.start_new_thread(reader, (slots,))
    for index in range(SLOTS):
        sync.thread.start_new_thread(writer, (slots, index, sleep))
    sync.Sync(2).sync()

if __name__ == '__main__':
    main()



Add comment

No comments.



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. a friendly mkdir()

4. SOLVING THE METACLASS ...

5. Povray for python

6. Changing return value ...

7. Implementation of sets ...

8. bag collection class

9. deque collection class

10. Floating Point Simulator




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.