mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Thread start

Sat Nov 28, 2015 2:52 pm

I'm at a standstill with a problem in my program and have tried everything.

The program samples the GPIO pins on a Raspberry pi in a very rapid loop, so that is one thread that is going on -- that part is working fine.

Now, I want to send an email while everything is happening (which includes a couple of threads, plus a tkinter interface etc -- much going on but as I say, all works fine.

I can set up a thread that will send the email fine but the problem is that after the email is sent, I have a timer (counter) that will wait 10 minutes, check status and send another email if needed.

The only way I can send the email is in a thread without it interfering with the program and the other loops etc. This business of threads can only be started once is baffling -- certainly there are situations like mine that have a solution? Any hints?

-- thanks much

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Thread start

Sat Nov 28, 2015 3:22 pm

Of course you can only start a thread once. Either let it end, or have it loop. Why not put a 10 minute loop in you email thread?

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Thread start

Sat Nov 28, 2015 4:09 pm

Yes -- that's what I'm wanting to do -- what i can't figure out is how to communicate with a thread that's running.

When I set up a class and declare variables I can use them anywhere (or at least have been able to so far).

example --

Code: Select all

class bozo:
        def __init__(self):
                self.go = False
                
then I can use bozo.go and manipulate it to either true or false telling the thread to either send the email again or not but although that works with other classes, it doesn't with a thread -- there was a thing called delegates in VBnet that would let you communicate with another thread but I don't know how with python. (hobbyist programmer -- not a pro). -- thanks

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Thread start

Sat Nov 28, 2015 4:15 pm

think I may have just fixed it -- I can setup a variable IN the thread and use it looks like. !!!!

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Thread start

Sat Nov 28, 2015 4:21 pm

Yes, you can use global variables to communicate between threads, or pass objects as arguments to the threaded function, or use queues. In lots of ways the latter is tidiest.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Thread start

Sat Nov 28, 2015 4:32 pm

yes -- I inititialized a variable in the thread itself and able to make it true / false outside of the thread, so that parts working. now to have the 10 minute counter/timer.

question --

as I say I have tkinter and python together, using root.after in a few places and already have time.sleep in another thread but it's running in microseconds mode in a while loop.

how many times can you use time.sleep in a program without any problems? if it's being used in different threads? i'd like to just do a while true loop in the thread and time sleep for 10 minutes -- trying that now and it seems to be ok.

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Thread start

Sat Nov 28, 2015 5:16 pm

Each thread can execute its own sleep independently, no problems with limits there.

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Thread start

Sat Nov 28, 2015 6:10 pm

Thanks Douglas -- that's great to hear -- see, it's things like that the books never tell you. Have it all working now and put it a 10 minute time sleep in the thread, so that's 3 different threads, all utilizing it.

This finishes a program I've been working months on little by little.

I have to say that Python and "friggin" Tkinter have been a bit of a pain sometimes but I gotta tell you that when the code works, it really works.

The only other language I taught myself was Vbnet and although easier to learn, weird shit can happen even if you don't see any errors thrown.

With Python, the one thing I've noticed is that if you don't see an error, it WORKS and works flawlessly 24/7

Is this my imagination or pretty much true?

Return to “Python”