Hello. I've just started learning Python and I have a few questions.
I'm using my program to communicate with some device through serial port using serial.py library. It's all going really fine.
I've got a class device, and I'm using it to communicate with a device (logical ;) ).
Q: is it better to open port in class __init__ method and close it in __del__,
or is it better to open-close it every time you send and receive a string through port?
Another question is why can't I re-start thread that has finished executing? My code for that is:
class thread1(threading.Thread):
def __init__ (self, br):
threading.Thread.__init__(self)
self.go = True
self.running = False
self.br = br
def run(self):
self.running = True
for i in range(1000):
global v
if self.go:
vlock.acquire()
v += self.br
vlock.release()
time.sleep(1)
else:
break
self.running = False
return 0
So, I start thread
t = thread1(1)
t.start()
,
let it run for a while, and then set
t.go = False
, which terminates thread. After that I try again to start
t.start()
, and it gives me
RuntimeError: thread already started
Is there any way you can do that, or I'm simply trying impossible?