{
 "metadata": {
  "name": "gilsleep"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Holding the GIL for too long could disrupt the heartbeat due to non-copying sends.\n",
      "\n",
      "The following cell repeatedly calls a function that holds the GIL for five seconds.\n",
      "\n",
      "The heartbeat will fail after a few iterations prior to fixing Issue [#1260](https://github.com/ipython/ipython/issues/1260)."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "import time\n",
      "\n",
      "from cython import inline\n",
      "\n",
      "def gilsleep(t):\n",
      "    \"\"\"gil-holding sleep with cython.inline\"\"\"\n",
      "    code = '\\n'.join([\n",
      "        'from posix cimport unistd',\n",
      "        'unistd.sleep(t)',\n",
      "    ])\n",
      "    while True:\n",
      "        inline(code, quiet=True, t=t)\n",
      "        print time.time()\n",
      "        sys.stdout.flush() # this is important\n",
      "\n",
      "gilsleep(5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": "&nbsp;"
    }
   ],
   "metadata": {}
  }
 ]
}