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?

Well, first off, t.go = False isn't going to have the effect you're trying to achieve, as you can see if you run print(t.isAlive()) after that line - the answer you'll get is True, whereas if you used t.join() (which causes the main thread to wait until t is completed), it will give False. However, even if you explicitly waited for t to end, you can't re-start a thread that's gone to completion; you would need to instantiate a new thread1 object and set t to that, instead.

I'm curious as to the overall purpose of this, though. What are you actually trying to accomplish with this?

Answering the first question second, I would say that it depends, but generally speaking, you would want to keep the serial device open rather than repeatedly opening and closing it. Opening and closing may not have that high an overhead, but they do have some, just as with opening and closing files.

Thanx for your answers.

Actually, t.go = False has really good effect, t.isAlive() gives me False.
I can't use t.join(), because I want to use thread t to write information from serial port to a file, every x seconds or so. At the same time, I have to be able to stop writing to file from main thread.
I'm pretty new to whole concept of threads, so maybe there's more simple solution.

Ah, I think I see what you need - this is related to what is called the 'Producers and Consumers' problem, a classic example of synchronization between different threads or processes. There are a number of articles around on how this works in Python, though they don't show exactly the solution you need.

In this case, I think the best solution is probably to set up a Condition variable that indicates when it is safe to write to the file, and have thread1 block when the condition is set. If you need to actually halt thread1 from main (as in stop it permanently), you may want to use another condition for that as well rather than simply killing the thread in the middle of it's actions.

It also has similarities to another classic problem, the 'Readers and Writers problem', so you may want to look at that as well.

Furthermore, you may want to use the queue class rather than writing your own code. This is something I should have mentioned before, sorry.

Thank you once again. I'll look it up

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.