title: How to use telethon in a thread
body: <p>I want to run a function in background. so I use Threading in my code.</p>
  
  <p>but return error <code>ValueError: signal only works in main thread</code> and don't know about two things:</p>
  
  <ol>
  <li>what is the main thread</li>
  <li>how to solve this problem :)</li>
  </ol>
  
  <p><code>views.py</code></p>
  
  <pre><code>def callback(update):
      print('I received', update)
  
  def message_poll_start():
      try:
          client = TelegramClient('phone', api_id, api_hash,
              update_workers=1, spawn_read_thread=False)
          client.connect()
          client.add_update_handler(callback)
          client.idle()
      except TypeNotFoundError:
          pass
  
  def message_poll_start_thread(request):
      t = threading.Thread(target=message_poll_start, args=(), kwargs={})
      t.setDaemon(True)
      t.start()
      return HttpResponse("message polling started")
  </code></pre>
  
  <p><code>urls.py</code></p>
  
  <pre><code>urlpatterns = [
      path('message_poll_start', messagemanager_views.message_poll_start_thread, name="message_poll_start"),
  ]
  </code></pre>
  
  <p><code>trace</code></p>
  
  <pre><code>[12/Jan/2018 11:24:38] "GET /messages/message_poll_start HTTP/1.1" 200 23
  Exception in thread Thread-3:
  Traceback (most recent call last):
    File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
      self.run()
    File "/usr/lib/python3.5/threading.py", line 862, in run
      self._target(*self._args, **self._kwargs)
    File "/home/teletogram/telethogram/messagemanager/views.py", line 123, in message_poll_start
      client0.idle()
    File "/home/teletogram/.env/lib/python3.5/site-packages/telethon/telegram_bare_client.py", line 825, in idle
      signal(sig, self._signal_handler)
    File "/usr/lib/python3.5/signal.py", line 47, in signal
      handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
  ValueError: signal only works in main thread
  </code></pre>