title: Python not releasing serial port
body: <p>I open the serial port, read some parameters from the modem (Omited here for simplicity), close the serial port and call sakis3g, a script to connect to Internet through a serial or USB modem.</p>
  
  <p>I can connect calling sakis3g from console.</p>
  
  <p>The code:</p>
  
  <pre><code># -*- coding: ISO-8859-1 -*-
  import serial
  import time
  from subprocess import call
  
  port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=0)
  port.open()
  at_ok=False 
  
  #wait until modem answers with an OK
  while at_ok==False:
      port.write(bytes("AT\n", 'ISO-8859-1'))
      time.sleep(.5)
      r=port.read(1000)
      if len(r)&gt;0:
          if r.decode("ISO-8859-1").find("OK")&gt;0:
              print ("Connected to modem")
              at_ok=True
  
  port.close()
  
  time.sleep(1) #just in case
  
  call(["sakis3g","connect","--pppd"])
  </code></pre>
  
  <p>The output:</p>
  
  <pre><code>root@raspberrypi:~# python3 /usr/test2.py
  Connected to modem
  Port /dev/ttyAMA0 is currently occupied by 14057 python3.
  Failed to connect.
  </code></pre>
  
  <p>(I'm using a raspberry)
  Edit: I used port.isOpen() just before the call, and it returns False.</p>
  
  <p>This is part of the output of sakis3g when I use the --debug option:</p>
  
  <pre><code>[20574] [16:57:04] We are root already. Proceeding.
  [20574] [16:57:04] Device /dev/ttyAMA0 is currently occupied by 0 process(es).
  [20574] [16:57:04] PID(s) are: 20542
  /-------------------------------------------------------------------------------
  [20574] [16:57:04] Will now run command: \'/bin/ps -p 20542 -o pid,comm= | /usr/bin/tail -1\'
  /-------------------------------------------------------------------------------
  20542 python3
  \-------------------------------------------------------------------------------
  [20574] [16:57:04] Command returned 0.
  \-------------------------------------------------------------------------------
  [20574] [16:57:04] Will wait for 10 seconds in case port is freed.
  [20574] [16:57:05] Verbosing: 21% Waiting /dev/ttyAMA0 to be released by PID 20542 python3.
  [20574] [16:57:05] Wait for another 10 seconds in case port /dev/ttyAMA0 is freed.
  </code></pre>
  
  <p>Edit2:
  I think I've found the problem (not the solution):
  I made a python script that do the folowing:
  - Wait 10 seconds
  - Open serial port
  - Wait another 10 seconds
  - Close serial port
  - Wait forever</p>
  
  <p>Using <code>lsof|grep ttyA</code> I get:</p>
  
  <p>before opening the port:</p>
  
  <pre><code>root@raspberrypi:~# lsof|grep ttyA
  root@raspberrypi:~#
  </code></pre>
  
  <p>When the port is open:</p>
  
  <pre><code>root@raspberrypi:~# lsof|grep ttyA
  python3     530            root    3u      CHR     204,64      0t0          9 /dev/ttyAMA0
  python3     530            root    4u      CHR     204,64      0t0          9 /dev/ttyAMA0
  root@raspberrypi:~#
  </code></pre>
  
  <p>After the port closes:</p>
  
  <pre><code>root@raspberrypi:~# lsof|grep ttyA
  python3     530            root    3u      CHR     204,64      0t0          9 /dev/ttyAMA0
  root@raspberrypi:~#
  </code></pre>
  
  <p>So pyserial or python is keeping the port occupied, but not locked?</p>