ricardo.santos
Posts: 43
Joined: Thu Jan 18, 2018 12:18 pm

threading using module EVENT

Thu Mar 08, 2018 4:56 pm

I have a code with 3 functions running as threads

read() and read_write() should run alternatively (the two are never in parallel)

detect() function establish which one of the two above thread functions is running and pauses the other thread

I've used Event to do this

Code: Select all

import threading
import numpy as np
from time import sleep

def read():
    event_rd.wait()
    print("read is running")

def read_write():
    event_rdw.wait()
    print("read_write is running")

def detect():
    while True:
          x= np.random.randint(1,100)
          if x > 50:
              event_rd.set()
          if x < 50:
              event_rdw.set()
          sleep(2)

event_rd = threading.Event  # restart read() and pause read_write()
event_rdw = threading.Event  # restart read_write ans pause read()

t_main = threading.Thread(target=detect, name="detect")
t_main.start()

t_rd = threading.Thread(target=read, name="read")
t_rdw = threading.Thread(target=read_write, name="read_write")
t_rd.start()
t_rdw.start()
Is not working why?

ricardo.santos
Posts: 43
Joined: Thu Jan 18, 2018 12:18 pm

Re: threading using module EVENT

Thu Mar 08, 2018 5:14 pm

the error log

Exception in thread read_write:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/home/pi/piwork/erosion-test/eventTest.py", line 10, in read_write
event_rdw.wait()
TypeError: wait() missing 1 required positional argument: 'self'

User avatar
Paeryn
Posts: 2712
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: threading using module EVENT

Thu Mar 08, 2018 6:30 pm

ricardo.santos wrote:
Thu Mar 08, 2018 4:56 pm
I have a code with 3 functions running as threads

read() and read_write() should run alternatively (the two are never in parallel)

detect() function establish which one of the two above thread functions is running and pauses the other thread

I've used Event to do this

Code: Select all

import threading
import numpy as np
from time import sleep

def read():
    event_rd.wait()
    print("read is running")

def read_write():
    event_rdw.wait()
    print("read_write is running")

def detect():
    while True:
          x= np.random.randint(1,100)
          if x > 50:
              event_rd.set()
          if x < 50:
              event_rdw.set()
          sleep(2)

event_rd = threading.Event  # restart read() and pause read_write()
event_rdw = threading.Event  # restart read_write ans pause read()

t_main = threading.Thread(target=detect, name="detect")
t_main.start()

t_rd = threading.Thread(target=read, name="read")
t_rdw = threading.Thread(target=read_write, name="read_write")
t_rd.start()
t_rdw.start()
Is not working why?
You created event_rd and event_rdw as being the class threading.Event rather than being objects of that class. You need to put brackets after the class name to create an object of a class, e.g.

Code: Select all

event_rd = threading.Event()  # restart read() and pause read_write()
event_rdw = threading.Event()  # restart read_write ans pause read()
You were getting the error because you were trying to call the function threading.Event.wait() directly with no object.
She who travels light — forgot something.

ricardo.santos
Posts: 43
Joined: Thu Jan 18, 2018 12:18 pm

Re: threading using module EVENT

Thu Mar 08, 2018 10:22 pm

:o
thanks a lot It works now

Return to “Python”