RLock to multi-thread synchronization

Problem scenario:
There are multiple threads, are responsible for the construction of houses, trees, gardening and the like work, which requires a certain waiting time, waiting for the process need to communicate with the server to stay connected. Submission can not be too frequent connection, preferably 2-3 seconds again.
For this synchronization between multiple threads can use RLock to achieve lock mechanism.

#coding=utf-8
'''
Created on 2010-9-14
@author: stonelee
'''
import time
import random
import threading
p_lock = threading.RLock()
s_lock = threading.RLock()

def my_post(str):
    p_lock.acquire()
    print str
    time.sleep(random.random() * 2)
    p_lock.release()

def my_sleep(seconds):
    s_lock.acquire()
    time.sleep(seconds)
    s_lock.release()    

def my_wait(name, seconds):
    last_time = time.time()
    last_seconds = 0
    while last_seconds < seconds:
        my_post('%s:%s' % (time.time(), name))
        my_sleep(3)
        last_seconds = time.time() - last_time

class ThreadA(threading.Thread):
    def run(self):
        for i in range(10000):
            my_post('%s:==================build house' % time.time())
            my_wait('a wait', 20)

class ThreadB(threading.Thread):
    def run(self):
        for i in range(10000):
            my_post('%s:=================plant tree' % time.time())
            my_wait('b wait', 5)

threadA = ThreadA()
threadA.setDaemon(True)
threadB = ThreadB()
threadB.setDaemon(True)

threadA.start()
threadB.start()
threadA.join()
threadB.join()
分类:Python 时间:2010-09-29 人气:138
分享到:
blog comments powered by Disqus

相关文章

  • Visual C + + thread synchronization analysis: critical, time, semaphores, mutexes, 2010-08-13

    Visual C + + thread synchronization analysis: critical, time, semaphores, mutexes, xuefeifei on 2006-1-12 10:43:00 Abstract: Multi-thread synchronization is an important technology computer software development, this paper various simultaneous multi-

  • Visual C + + thread synchronization analysis: the critical zone, time, semaphores, mutexes, 2010-10-18

    The thread synchronization The use of multiple threads in the program, generally there is little more than one thread can be carried out during the period of their lives completely separate operation. More number of threads is that some processing op

  • Java thread: Thread synchronization - sync block 2010-08-04

    Java thread: Thread synchronization - sync block For synchronization, in addition to synchronization, but also can use the synchronized block of code, and sometimes simultaneous block will bring better results than the simultaneous method. Pursuit of

  • Semaphore thread synchronization to solve the classic problem 2010-08-17

    Semaphore is EWDijkstra in l965, proposed a method that uses an integer variable to the cumulative wake-up times for future use. In his proposal to introduce a new type of variable number, called semaphore (semapore). The value of a semaphore can be

  • About Thread Synchronization 2010-03-29

    java thread synchronization may be implemented in ways people think of join. But here not talking about it. But rather that the two tools. I copy the files directly from the jd to be here. ////////////////////////////////////////////////// //////////

  • Java thread of thread synchronization 2010-03-29

    Note: wait notify methods are Object synchronization (blocking): is a shared resource to prevent access to data inconsistencies resulting from a model. See more the operating system. In Java, because of multi-threading support, mainly through the syn

  • JAVA thread synchronization 2010-04-05

    We can run on a computer a variety of computer software programs. Each running program may include multiple independent threads running (Thread). Threads (Thread) is a stand-alone program, has its own dedicated run-time stack. Thread and other thread

  • java thread synchronization with asynchronous thread pool 2009-04-07

    Thread synchronization, the concept of asynchronous 1) Multi-threaded concurrency, multiple threads simultaneously requesting the same resources, will inevitably lead to the data for this resource insecurity, A modified B-threaded thread-processed da

  • Thread Synchronization 2010-03-28

    Thread Synchronization We can run a variety of computer software programs. Each run of the program may include multiple independent threads running (Thread). Threads (Thread) is a stand-alone program, run their own dedicated stack. Thread and other t

  • [Multi-threaded programming] profoundly expounded thread synchronization mechanism 2010-05-16

    The full text: http://www.javaeye.com/topic/164905 Author: buaawhl We can run a variety of computer software programs. Each run of the program may include multiple independent threads running (Thread). Threads (Thread) is a stand-alone program, run t

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.163 (s). 12 q(s)