python3: issue with __del__ when script terminates
See original GitHub issueAfter picking up Denis’s ‘Restore Python3 compatibility’ fix, with python3 I observed weird issue related to del behavior.
It could be related to some race condition when python3 goes down. In one case ‘isabs’ function from posixpath.py becomes None. So on call chain triggered by Motor del -> stop … it fails. In other case see below ‘open’ function disappeared.
(ev3dev_py34)root@ev3dev:~/projects/v/test2# cat del_test.py
import ev3dev.ev3 as ev3
m = ev3.LargeMotor("outB")
exit()
(ev3dev_py34)root@ev3dev:~/projects/v/test2# python3 del_test.py
Exception ignored in: <bound method LargeMotor.__del__ of <ev3dev.core.LargeMotor object at 0xb6a14b30>>
Traceback (most recent call last):
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 221, in **del**
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 670, in stop
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 235, in command
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 178, in set_attr_string
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 166, in _set_attribute
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/posixpath.py", line 357, in abspath
TypeError: 'NoneType' object is not callable
With some slight variation with the test issue disappears:
(ev3dev_py34)root@ev3dev:~/projects/v/test2# cat del_test1.py
import ev3dev.ev3 as ev3
def main():
m = ev3.LargeMotor("outB")
main()
(ev3dev_py34)root@ev3dev:~/projects/v/test2# python3 del_test1.py
(ev3dev_py34)root@ev3dev:~/projects/v/test2# cat del_test2.py
import ev3dev.ev3 as ev3
def main():
m = ev3.LargeMotor("outB")
exit()
main()
(ev3dev_py34)root@ev3dev:~/projects/v/test2# python3 del_test2.py
(ev3dev_py34)root@ev3dev:~/projects/v/test2# cat del_test3.py
import ev3dev.ev3 as ev3
m = ev3.LargeMotor("outB")
(ev3dev_py34)root@ev3dev:~/projects/v/test2# python3 del_test3.py
(ev3dev_py34)root@ev3dev:~/projects/v/test2#
It seems that it could be races in the code in other case ‘open’ name disappeared:
(ev3dev_py34)root@ev3dev:~/projects/v/test2# python3 del_test.py
Exception ignored in: <bound method LargeMotor.__del__ of <ev3dev.core.LargeMotor object at 0xb6a2eb30>>
Traceback (most recent call last):
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 221, in **del**
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 670, in stop
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 235, in command
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 178, in set_attr_string
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 166, in _set_attribute
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 88, in write
File "/root/.virtualenvs/ev3dev_py34/lib/python3.4/site-packages/python_ev3dev-0.5.0.post3-py3.4.egg/ev3dev/core.py", line 74, in file_handle
NameError: name 'open' is not defined
When the same tried with python2 everything looks fine.
By googling info about del I found this:
https://docs.python.org/2/reference/datamodel.html#object.__del__
The pink section pretty much explains the issues I observe, which begs the question whether it is good idea to have del method in the first place.
On one hand desire to stop all motors when cotrolling process exits, especially abnormally, is well understood. But needless to say that if above condition hit when motor was running it won’t stop anyway.
I wonder whether ev3dev infra (file interface) can provide more solid emergency stop mechanism. For example stop could be trigger on close of some special file, like /sys/class/tacho-motor/motor?/emergency_stop. If such file would be opened, ev3dev system will stop motor when file closes. In this case python can just open the file in Motor contructor and file will be automatically closed when python exists, that will trigger motors stop. It would stop the motors even if process explicitly killed.
Other language bindings can use the same mechanism.
Issue Analytics
- State:
- Created 8 years ago
- Comments:14 (11 by maintainers)
@ddemidov do you think this one is safe to close? The only
__del__
left is in FbMem, I haven’t heard mention of any issues with it.Hi Ralph,
On Tue, Jan 5, 2016 at 2:32 PM, Ralph Hempel notifications@github.com wrote:
No, I am not a teacher. We had FLL team that I coached. After 3 seasons of FLL with EV3 graphical environment programming we decided to move further. So with elder part of previous team (my son and two of his friends) kids are trying to learn real programming language while still doing some robotics. And of course we have all pieces around from FLL. Experience with EV3 graphical programming environment does help, very often kids relate back to it.
IMHO python is very good start as programming language. I personally recommend the following book:
http://www.amazon.com/gp/product/1435455002?keywords=python programming for the absolute beginner&qid=1452043541&ref_=sr_1_1&sr=8-1 “Python Programming for the Absolute Beginner, 3rd Edition” by Michael Dawson
That one is about writing games with python initially text based and simple graphical at the end, while introducing all basic programming concepts on the way. We use that one as python “textbook” but we try to apply what we learned to robotic related tasks so it is more fun and familiar problem set. Using ev3dev python module kids are writing functions to drive in cm/inches (rather than motor rotations), make robot turns in degrees, drive some specified route, writing robot class, drive with sensors on FLL field following line, etc. Some simple things so far.
LeJOS was another choice, but I find that learning basic programming skills with Java is much harder compared to python.
While we have this thread: I just want to say big thank you to all you guys! You are doing great job with ev3dev! It is exactly what I’ve been looking for. Very much appreciated.
Thanks, Victor
I’ll look at getting these exceptions fixed in the next pass at the