Title: Make Turtle thread-safe so it does not crash
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 3.2
Status: open Resolution:
Dependencies: Superseder: turtle: _tkinter.TclError: invalid command name ".10170160"
View: 6639
Assigned To: Nosy List: belopolsky, gregorlingl, josiahcarlson, korka, rhettinger
Priority: normal Keywords:

Created on 2007-04-17 08:29 by korka, last changed 2010-11-05 02:19 by belopolsky.

Messages (5)
msg55062 - (view) Author: lomm (korka) Date: 2007-04-17 08:29
These are a few examples of errors during code-reentry in the turtle module:

System tested: Windows XP sp2, Python 2.5

1. fails if the tkinter is closed while drawing.

# Code example:
import turtle
# close the tkinter window while the circle is drawing
# will give an "invalid command name" exception

2. using multiple inheritance, it's possible to draw more than 1 turtle running around at a time. This works part of the time, but crashes python completely on occasions.

# Code example:
import turtle, random
from threading import Thread

class Ninja(Thread, turtle.Turtle):
	'A ninja is a threaded turtle'
	def __init__(self):
		# constructors
		# where will i go?
		self.Direction = random.randint(-180,180)
	def run(self):
		# that way!
		# march 'forward'
		for i in range(50):
			self.left(22 - 45*random.random())

ninjas = []
for i in range(3):
msg55063 - (view) Author: Josiah Carlson (josiahcarlson) * Date: 2007-04-19 05:12
Does Turtle claim to be thread safe in the documentation?  Should we explicitly state that Turtle is not thread safe?  Should we also say, "don't close the turtle window while it is drawing"?
msg55064 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-04-19 06:58
As Josiah says, the current docs do not make any promises about thread-safety.  So, this isn't a bug.

That being said, it would be nice to offer a way to have multiple turtles running around under the control of different threads.
msg120464 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-11-05 02:08
I think threading is a red herring here.  The issue is really a duplicate of #6639.
msg120465 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2010-11-05 02:19
On a closer look at the first post, I see that there are two parts to the issue.  The first is indeed a duplicate of #6639, but the second is thread related.   I am attaching the OP's code in a script file for convenience.  Running under python3 produces 

RuntimeError: main thread is not in main loop

in every ninja thread.  This looks like tkinter limitation, but I think it is possible to work around it in turtle, so this is a valid feature request.

Date User Action Args
2010-11-05 02:19:39belopolskysetstatus: pending -> open

messages: + msg120465
2010-11-05 02:08:18belopolskysetstatus: open -> pending

nosy: + belopolsky
messages: + msg120464

superseder: turtle: _tkinter.TclError: invalid command name ".10170160"
2010-10-27 13:35:41belopolskysetnosy: + gregorlingl
2010-08-09 03:44:29terry.reedysettitle: Turtle isn't thread-safe (crashes) -> Make Turtle thread-safe so it does not crash
versions: + Python 3.2, - Python 3.1, Python 2.7
2009-03-30 16:49:34ajaksu2setstage: test needed
versions: + Python 3.1, Python 2.7, - Python 2.6
2007-04-17 08:29:04korkacreate