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.

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):
    print str
    time.sleep(random.random() * 2)

def my_sleep(seconds):

def my_wait(name, seconds):
    last_time = time.time()
    last_seconds = 0
    while last_seconds < seconds:
        my_post('%s:%s' % (time.time(), name))
        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()
threadB = ThreadB()

分类:Python 时间:2010-09-29 人气:134
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

  • Design java multi-thread synchronization wait / notify mechanism (more practical) 2010-07-30

    Design java multi-thread synchronization wait / notify mechanism (more practical) 294 have been reading 2009-08-26 17:27 Design java multi-thread synchronization wait / notify mechanism Need for coordination among multiple threads. For example, the b

  • 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

  • Detailed rules for multi-thread synchronization [I] 2010-11-27

    Transfer from http://earthrelic.blog.sohu.com/157151118.html Familiar with Java's multi-threaded data inconsistencies are generally known to happen there, such as two threads in the operation of the same class variable, while protecting the data with

  • 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

  • Simple usage of multi-thread synchronization 2010-03-29

    Usual in making thread synchronization, we use synchronized methods, but relatively large in the project when the classes are often created by a third party, then how to maintain the thread synchronization it? Can be placed in such ways to access the

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



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

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

processed in 0.087 (s). 14 q(s)