Error occur for sys.exit()

Asked by Jie Qin

When I simply repeat the ''Gravity Deposition" example by adding a exit function, following errors occur. The exit function is simply

import sys
sys.exit()

-------------------
Exception KeyError: KeyError(140000453338880,) in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2971, in atexit_operations
    self.history_manager.end_session()
  File "/usr/lib/python2.7/dist-packages/IPython/core/history.py", line 432, in end_session
    self.writeout_cache()
  File "<string>", line 2, in writeout_cache
  File "/usr/lib/python2.7/dist-packages/IPython/core/history.py", line 60, in needs_sqlite
    return f(*a,**kw)
  File "/usr/lib/python2.7/dist-packages/IPython/core/history.py", line 607, in writeout_cache
    self._writeout_input_cache(conn)
  File "/usr/lib/python2.7/dist-packages/IPython/core/history.py", line 591, in _writeout_input_cache
    (self.session_number,)+line)
ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 140001294083904 and this is thread id 140000453338880
Error in sys.exitfunc:
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
/usr/lib/python2.7/atexit.pyc in _run_exitfuncs()
     22 func, targs, kargs = _exithandlers.pop()
     23 try:
---> 24 func(*targs, **kargs)
     25 except SystemExit:
     26 exc_info = sys.exc_info()

/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in atexit_operations(self)
   2969 # this must be *before* the tempfile cleanup, in case of temporary
   2970 # history db
-> 2971 self.history_manager.end_session()
   2972
   2973 # Cleanup all tempfiles left around

/usr/lib/python2.7/dist-packages/IPython/core/history.pyc in end_session(self)
    430 def end_session(self):
    431 """Close the database session, filling in the end time and line count."""
--> 432 self.writeout_cache()
    433 with self.db:
    434 self.db.execute("""UPDATE sessions SET end=?, num_cmds=? WHERE

/usr/lib/python2.7/dist-packages/IPython/core/history.pyc in writeout_cache(self, conn)

/usr/lib/python2.7/dist-packages/IPython/core/history.pyc in needs_sqlite(f, *a, **kw)
     58 return []
     59 else:
---> 60 return f(*a,**kw)
     61
     62

/usr/lib/python2.7/dist-packages/IPython/core/history.pyc in writeout_cache(self, conn)
    605 with self.db_input_cache_lock:
    606 try:
--> 607 self._writeout_input_cache(conn)
    608 except sqlite3.IntegrityError:
    609 self.new_session(conn)

/usr/lib/python2.7/dist-packages/IPython/core/history.pyc in _writeout_input_cache(self, conn)
    589 for line in self.db_input_cache:
    590 conn.execute("INSERT INTO history VALUES (?, ?, ?, ?)",
--> 591 (self.session_number,)+line)
    592
    593 def _writeout_output_cache(self, conn):

ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 140001294083904 and this is thread id 140000453338880

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Jie Qin
Solved:
Last query:
Last reply:
Revision history for this message
Klaus Thoeni (klaus.thoeni) said :
#1

Not sure what's going on here, exit() should do the job. I tried it with one of my scripts and it's working.

# your script...
O.run(1000,True)
exit()

But why do you want to exit? If you are just running a deposition and want to store it I would recommend to use yade-batch instead of yade since it automatically exits when the script is finished.

HTH
Klaus

Revision history for this message
Jie Qin (qinjie1629) said :
#2

Hi, Klause, thank you for your reply.

Firstly, like the following "bouncing spheres", the example provided by Yade, when I add a exit() function, the errors occur.

Secondly, I do want to use yade-batch to simulate multiple situations and that is why I want to use the exit function. I do not catch what is your meaning that it automatically exits, because I was just following the tutorials of yade-batch method.

---------------------------------bouncing spheres----------------------------------------------------------
O.bodies.append([
   utils.sphere((0,0,0),.5,fixed=True),
   utils.sphere((0,0,2),.5)
])

O.engines=[
   ForceResetter(),
   InsertionSortCollider([Bo1_Sphere_Aabb()]),
   InteractionLoop(
      [Ig2_Sphere_Sphere_L3Geom()],
      [Ip2_FrictMat_FrictMat_FrictPhys()],
      [Law2_L3Geom_FrictPhys_ElPerfPl()]
   ),
   NewtonIntegrator(gravity=(0,0,-9.81),damping=0.2),
   PyRunner(command='checkPosition()',realPeriod=2)
]
O.dt=0.5e-3*utils.PWaveTimeStep()

def checkPosition():
   if utils.unbalancedForce()<0.001:
      import sys
      sys.exit()

----------------------------------yade-batch tutorial---------------------------------------------
O.engines+=[PyRunner(iterPeriod=1000,command='checkUnbalancedForce()')] # call our function defined below periodically

def checkUnbalancedForce():
   if utils.unbalancedForce<0.05: # exit Yade if unbalanced force drops below 0.05
      utils.saveDataTxt(O.tags['d.id']+'.data.bz2') # save all data into a unique file before exiting
      import sys
      sys.exit(0) # exit the program

Revision history for this message
Klaus Thoeni (klaus.thoeni) said :
#3

I see, try O.exitNoBacktrace() instead.

HTH Klaus

Revision history for this message
Jie Qin (qinjie1629) said :
#4

Hi, Klaus, it works, Thank you!