question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

python3: issue with __del__ when script terminates

See original GitHub issue

After 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:closed
  • Created 8 years ago
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
dwalton76commented, Sep 15, 2016

@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.

0reactions
victorkamenskycommented, Jan 6, 2016

Hi Ralph,

On Tue, Jan 5, 2016 at 2:32 PM, Ralph Hempel notifications@github.com wrote:

Victor, are he kids your own, or are you a teacher? I am very interested in hearing about how you manage to teach 7th Graders to code using Python.

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

bindings.

— Reply to this email directly or view it on GitHub https://github.com/rhempel/ev3dev-lang-python/issues/96#issuecomment-169155897 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python script is stopped due to "Terminated" in linux
I don't know why my script is stopped and show only "Terminated" message without getting any error message. Therefore I run strace -p...
Read more >
atexit — Exit handlers — Python 3.11.1 documentation
exit() is called or the main module's execution completes), all functions registered are called in last in, first out order.
Read more >
Known issues for Python and R - SQL Server Machine ...
Running a long running external script session and having the dbo in parallel trying to install a library on a different database can...
Read more >
How to Terminate a running process on Windows in Python?
Terminating a concurrently running process should be done appropriately and with a ... wmic process where name="Process_Name" delete.
Read more >
How to stop a program in Python – with example - CodeBerry
Exiting a program is the process of terminating an active python program from ... all the code in that particular script has been...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found