0

i have threads starting point

for _ in xrange(THREADS_COUNT):
        global thread_
        thread_ = threading.Thread(target=self.mainWork, args=(mainProject,victims))
        thread_.start()
        time.sleep(5)

and i need to lock all threads but one(in which happened if) when if occur.

if 'di_unread_inbox' in inbox_page:
    ...

and when else condition occur, i need to unlock threads if them are locked(check for locking required i think)

Alice Polansky
  • 3,231
  • 3
  • 18
  • 25
  • Can you actually write a sample `mainWork` function that shows the full `if`/`else` block you want here? It's not quite clear to me what you're looking for here. – dano Oct 16 '14 at 17:48
  • well mainWork its just method of a class. i just need to stop all threads but one and can start them again. – Alice Polansky Oct 16 '14 at 17:51
  • Duplicate of [Pausing a thread using threading class](http://stackoverflow.com/questions/3262346/pausing-a-thread-using-threading-class). See especially [Alex Martelli's answer](http://stackoverflow.com/a/3262410/1322401). – Steven Rumbalski Oct 16 '14 at 17:54
  • @StevenRumbalski well and what about http://stackoverflow.com/questions/10525185/python-threading-how-do-i-lock-a-thread-or-do-i-even-lock-the-thread-or-just ? – Alice Polansky Oct 16 '14 at 17:58
  • @RomaPerfilyev: Locks allow threads to cooperate when accessing a shared resource. In no way does that mean that one thread pauses or stops another. – Steven Rumbalski Oct 16 '14 at 18:02
  • @StevenRumbalski Well, all other instances of `mainWork` will effectively be "paused" as long as one instance is holding the `Lock`. It's hard to tell if that's actually what the OP is talking about here, though. – dano Oct 16 '14 at 18:04
  • @dano: They are "paused" by consent. That is different than telling another thread to pause. – Steven Rumbalski Oct 16 '14 at 18:05
  • @StevenRumbalski Right, but I'm not sure if the OP is saying "I want to be able to pause all other threads at whatever arbitrary point of execution they're at when I enter this `if` block", or "I want only one thread to be able to run inside of this `if` block at a time. Other threads should wait until there is no other thread in that block before entering themselves." – dano Oct 16 '14 at 18:09
  • @StevenRumbalski well i have shared resourse - **inboxmessagelabel.set('Нет новых сообщений')** - and when in one thread **if** occurs **inboxmessagelabel** changes - set other value, but other threads change inboxmessagelabel again cauze them works all time. – Alice Polansky Oct 16 '14 at 18:13
  • @RomaPerfilyev It sounds like you can just use a `threading.Lock` to handle that. – dano Oct 16 '14 at 18:16
  • @RomaPerfilyev: If you need to control access to a shared resource then `threading.Lock` is what you need. – Steven Rumbalski Oct 16 '14 at 18:16
  • @StevenRumbalski Are you ok with unduping this question, since it seems the question isn't actually about pausing threads? – dano Oct 16 '14 at 18:21
  • @StevenRumbalski yes, and how can i do that? for example i have var **i** = 2. all threads executing functions in which code is `x = 2`. but when __if__ condition occurs in one thread - **i** will change to 3 but other threads change i to 2 again quickly! how can i avoid this? – Alice Polansky Oct 16 '14 at 18:23
  • @RomaPerfilyev Acquire a lock before executing the `if` condition. – dano Oct 16 '14 at 18:25

1 Answers1

3

You need to acquire a lock before checking the if condition, and then release it once you've either 1) updated the shared resource, or 2) determined the resource doesn't need updating, and another logic branch should be used. That logic looks like this:

lock = threading.Lock()


def mainWork():
    # do stuff here
    lock.acquire()
    if 'di_unread_inbox' in inbox_page:
        try:
            # something in here changes inbox_page so that 'di_unread_inbox' isn't there anymore
            inboxmessagelabel.set("some label")
        finally:
            lock.release()
    else:
        lock.release()
        # do other stuff

If you don't need the else block, the logic looks a bit simpler:

def mainWork():
    # do stuff here
    with lock:
        if 'di_unread_inbox' in inbox_page:
            inboxmessagelabel.set("some label")

    # do other stuff
dano
  • 91,354
  • 19
  • 222
  • 219