feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

threading

Wed Sep 10, 2014 2:31 pm

http://sentdex.com/sentiment-analysisbi ... threading/

The above URL says it is a tutorial on a type of threading in Python, and provides a movie and textual example of its use. This relies on importing 'thread', but I cannot see such a package and apt-get does not fetch one, plus the package list does not seem to contain any such animal. Can anyone please point me in the right direction?
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

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

Re: threading

Wed Sep 10, 2014 2:50 pm

'thread' is included with Python. What happens when you enter 'import thread' at the Python prompt?

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

Re: threading

Wed Sep 10, 2014 4:39 pm

I remember exactly why but it's better to use the higher level
import threading
see here for docs
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

Re: threading

Wed Sep 10, 2014 5:58 pm

Douglas6 wrote:'thread' is included with Python. What happens when you enter 'import thread' at the Python prompt?
Idle does not report an error, Idle3 says ImportError:No module named thread.
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

Re: threading

Wed Sep 10, 2014 6:09 pm

paddyg wrote:I remember exactly why but it's better to use the higher level
import threading
see here for docs
Thank you. Reading the docs now. Maybe python 3.2.3 doesn't like it, but 2.7.3 does? I need it to work in 3.2.3 so more srudy needed.
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: threading

Wed Sep 10, 2014 6:30 pm

If you look at the docs for the thread module it says this:
Note The thread module has been renamed to _thread in Python 3

But, as Paddyg says, I think the recommendation is now to use the threading module.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

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

Re: threading

Wed Sep 10, 2014 6:32 pm

paddyg is correct again; 'thread' has been deprecated, and removed (hidden, actually) from Python3. Here's that (not very good) example updated for Python3

Code: Select all

from threading import Thread
import time
import random


def runOften(threadName, sleepTime):
    while True:
        time.sleep(sleepTime)
        print (threadName)

def runLessOften(threadName, sleepTime):
    while True:
        time.sleep(sleepTime)
        print (threadName)

def runRandomly(threadName, sleepTime):
    while True:
        time.sleep(sleepTime)
        print (threadName)

try:
    Thread(target=runOften, args=("Often Runs", 2)).start()
    Thread(target=runLessOften, args=("Less often runs!", 5)).start()
    Thread(target=runRandomly, args=("FAST and Random", 2)).start()
except Exception as e:
    print(e)


feverish
Posts: 486
Joined: Wed Jun 27, 2012 2:29 pm

Re: threading

Thu Sep 11, 2014 8:54 am

My thanks to you experts who so tirelessly teach python beginners like me the contrary aspects of 'tutorials' on the web when Python is involved.
If discrimination is not challenged then we are effectively in collusion with the perpetrators of such behaviour:-Oxford dictionaries

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: threading

Sat Sep 26, 2015 3:31 pm

Douglas6 wrote:paddyg is correct again; 'thread' has been deprecated, and removed (hidden, actually) from Python3. Here's that (not very good) example updated for Python3

Code: Select all

from threading import Thread
import time
import random


def runOften(threadName, sleepTime):
    while True:
        time.sleep(sleepTime)
        print (threadName)

def runLessOften(threadName, sleepTime):
    while True:
        time.sleep(sleepTime)
        print (threadName)

def runRandomly(threadName, sleepTime):
    while True:
        time.sleep(sleepTime)
        print (threadName)

try:
    Thread(target=runOften, args=("Often Runs", 2)).start()
    Thread(target=runLessOften, args=("Less often runs!", 5)).start()
    Thread(target=runRandomly, args=("FAST and Random", 2)).start()
except Exception as e:
    print(e)

Sorry to dig up an old conversation, but does this mean that if I want to create an application containing threading I ought to be using something other than python3?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: threading

Sat Sep 26, 2015 3:36 pm

no. You just need to use the threading module.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: threading

Sat Sep 26, 2015 3:57 pm

You need to be aware that on a pi2 threading will not make use of the 4 cores. Google "Global interpreter lock'. The manual suggests using the multiprocessing module instead.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: threading

Sat Sep 26, 2015 4:04 pm

Indeed. I guess it just depends on what you're trying to achieve. The threading module has always been enough for me.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: threading

Sat Sep 26, 2015 4:20 pm

Thanks - I'm using a piB+, so the multicore bit doesn't bother me.
Time to crack on with the threading module then :D

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: threading

Sun Sep 27, 2015 5:13 pm

OK I wonder if someone could give me a push in the right direction. I want a routine in python3 which when I press the 'go' button it will sit and read data from the serial port (and do other stuff with it) until the program is exited.
I started off with something like:

Code: Select all

def go():
    while true:
         do all bits for serial port in here
.
.
.
gobutton = tkinter.Button (window, text="start", command = go)

which sort of worked, but as you can imagine it locked out everything else and the only way to exit was ^C.
So, I imported threading but if I just replace go with go(worker) I get errors about the number of arguments in go. It looks as though I need to set up a queue of some sort, but I can't figure out how to do that from within the 'command=' part of my button call.

Thanks for any help (yes, you're right - I haven't fully grasped how threading is supposed to be used)

Paul

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

Re: threading

Sun Sep 27, 2015 6:43 pm

it's a bit hard without knowing what go() is trying to do. queue can be used to pass and or receive info from the thread asynchronously and is really needed where you put functionality into subprocesses. Your code doesn't start the function in a thread. Maybe you're trying to do something like

Code: Select all

def go():
    while true:
         do all bits for serial port in here
.
.
def start_go():
  t = threading.Thread(target=go)
  t.start()
.
gobutton = tkinter.Button (window, text="start", command = go)
the easiest way to pass info is by having global variables but it's better in the long run to get used to passing arguments which the threaded function can update.

PS you either need a sleep inside you thread while loop or something that blocks (like reading from serial)

PPS quite often you don't need threads at all!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: threading

Sun Sep 27, 2015 9:58 pm

Thanks for the reply.
Initially, 'go' is just going to read the serial port and display the data - later versions will do a bit more - write to the port too and perform calculations on the received data.
Your example appears to make sense - except should the button command now call start_go instead?
Thanks again.

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

Re: threading

Sun Sep 27, 2015 10:12 pm

:oops: - good job you're paying attention!

One other thing: you ought to have a tidying up system to stop the thread, say checking a global variable or one the argument objects. If you don't then you should t.daemon = True (before start()) which will allow the interpreter to stop the thread when the program stops.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: threading

Mon Sep 28, 2015 6:08 pm

I do try to pay attention is someone is being good enough to explain things to me :D
I've got a 'quit' button which calls a routine which does some tidying up, so I guess I can stop any threads in there...

Cheers.

Return to “Python”