nios2
Posts: 38
Joined: Mon Mar 12, 2018 3:22 am

Thread behaviour in Idle and Thonny

Sun Apr 22, 2018 3:32 am

Dear all,

This may not be the correct forum but hopefully someone maybe able to help. I am investigating simple threads to be able to have a thread waiting for keyboard input running along with other threads that are doing some processing.

The 2 threads program below executes as I expect in Thonny i.e. the f2 thread runs around the loop and the f1 thread only loops on receipt of an enter key.

In Idle the entire program halts awaiting the enter key for f1 giving the impression that the input() function does not yield allowing the f2 thread to run.

My questions are:-

1) Is the underlying Python engine the same in both Idle and Thonny?

2) Are some Python functions "blocking" in that they cannot be thread switched within the function?


Code: Select all

import _thread # _ in lib name means old lib 
import time

#global x
x = 5

# Define functions for the threads
def f1( threadName, delay): #delay not used here
   global x 
   while 1:
      
      while input() == None : #waits for input
                             pass
      x = x + 1                           #update global
      print( "%s: %d" % ( threadName, x)) #print

def f2(threadName, delay):
    while 1:
     print ("%s: %d" % (threadName, x))
     time.sleep(delay)  #pause for delay

# Create two threads as follows
try:
    _thread.start_new_thread( f1, ("Thread-1", 1) ) 
    _thread.start_new_thread( f2, ("Thread-2", 1) ) #start thread and pass delay

except:
   print ("Error: unable to start thread") # does this line if the above fails

while 1:
   pass  #equiv to a nop
Last edited by nios2 on Sun Apr 22, 2018 10:27 am, edited 1 time in total.

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

Re: Thread behaviour in Idle and Thonny

Sun Apr 22, 2018 8:11 am

Please use code tags when posting code

Never used Thonny but have you tried using a small sleep in your loop rather than pass? You could be maxing out your CPU.

Also, why aren't you using the proper threading module? Isn't the one you're using a dummy module?

EDIT: maybe the problem is just the way IDLE handles threads. Never used it so can't confirm but if your script rubs properly elsewhere then that does suggest that IDLE is the problem here.
Last edited by elParaguayo on Sun Apr 22, 2018 8:25 am, edited 1 time in total.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
DougieLawson
Posts: 36307
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Thread behaviour in Idle and Thonny

Sun Apr 22, 2018 8:19 am

If you're going to post python can you wrap it in [code]... your stuff goes here ...[/code] tags.

That makes the difference between
if code_tags != "USED":
print ("formatting is lost")
else:
print ("python looks ok")
and

Code: Select all

if code_tags != "USED":
  print ("formatting is lost")
else:
  print ("python looks ok")
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

nios2
Posts: 38
Joined: Mon Mar 12, 2018 3:22 am

Re: Thread behaviour in Idle and Thonny

Sun Apr 22, 2018 8:25 am

Sorry about the tags, it was my 1st post of code and I didn't realise until after it appeared un-indented. I should have read the sticky.

User avatar
DougieLawson
Posts: 36307
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Thread behaviour in Idle and Thonny

Sun Apr 22, 2018 8:31 am

nios2 wrote:
Sun Apr 22, 2018 8:25 am
Sorry about the tags, it was my 1st post of code and I didn't realise until after it appeared un-indented. I should have read the sticky.
Can you edit your original post? There's an edit button ====================================== up there =========^^^^^ top right of your post.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
bensimmo
Posts: 4182
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Thread behaviour in Idle and Thonny

Sun Apr 22, 2018 8:49 am

Just checking you are using IDLE3 and not IDLE, as Thonny is Python3 on Raspbian, by default.
It uses the same Python in the background (as far as I know) on Raspbian, it makes it easy for people too switch between them and get the modules.

(Normally Thonny bundles Python3 with it, if you install it on Windows etc)

nios2
Posts: 38
Joined: Mon Mar 12, 2018 3:22 am

Re: Thread behaviour in Idle and Thonny

Sun Apr 22, 2018 11:27 am

bensimmo wrote:
Sun Apr 22, 2018 8:49 am
Just checking you are using IDLE3 and not IDLE, as Thonny is Python3 on Raspbian, by default.
It uses the same Python in the background (as far as I know) on Raspbian, it makes it easy for people too switch between them and get the modules.

(Normally Thonny bundles Python3 with it, if you install it on Windows etc)
Thanks for the suggestion, I am Python 3 with all tool.

nios2
Posts: 38
Joined: Mon Mar 12, 2018 3:22 am

Re: Thread behaviour in Idle and Thonny

Sun Apr 22, 2018 11:28 am

DougieLawson wrote:
Sun Apr 22, 2018 8:31 am
nios2 wrote:
Sun Apr 22, 2018 8:25 am
Sorry about the tags, it was my 1st post of code and I didn't realise until after it appeared un-indented. I should have read the sticky.
Can you edit your original post? There's an edit button ====================================== up there =========^^^^^ top right of your post.
Thanks, I have done that now, as you pointed out it was very easy.

User avatar
bensimmo
Posts: 4182
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Thread behaviour in Idle and Thonny

Sun Apr 22, 2018 12:06 pm

elParaguayo wrote:
Sun Apr 22, 2018 8:11 am
Also, why aren't you using the proper threading module? Isn't the one you're using a dummy module?
_thread python 3.5
https://docs.python.org/3.5/library/_thread.html
But most user the higher threading
https://docs.python.org/3.5/library/thr ... -threading

I'm no expert in either.

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

Re: Thread behaviour in Idle and Thonny

Sun Apr 22, 2018 4:31 pm

the convention in python is that properties or methods named with an initial underscore are to be treated as privateish. Use them if you want but make sure you have a thorough knowledge of the module source code and know exactly what the consequences will be.

ps I have found that a small number of things just don't work in IDLE. Also I would second elP's suggestion to put in a small sleep. Python threading adds quite an overhead of processing work (Google GIL) so you might be creating strange byproduct.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

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

Re: Thread behaviour in Idle and Thonny

Sun Apr 22, 2018 5:06 pm

bensimmo wrote:
Sun Apr 22, 2018 12:06 pm
elParaguayo wrote:
Sun Apr 22, 2018 8:11 am
Also, why aren't you using the proper threading module? Isn't the one you're using a dummy module?
_thread python 3.5
https://docs.python.org/3.5/library/_thread.html
But most user the higher threading
https://docs.python.org/3.5/library/thr ... -threading

I'm no expert in either.
Yup, my mistake. Saw that in the docs after I'd posted but it was too fiddly to edit my post on my phone. Thanks for posting the link.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”