Skip to content

Instantly share code, notes, and snippets.

@umaxfun
Created May 25, 2018 09:52
Show Gist options
  • Save umaxfun/f2a6bbf331d3da919196a4c6c83dd2a7 to your computer and use it in GitHub Desktop.
Save umaxfun/f2a6bbf331d3da919196a4c6c83dd2a7 to your computer and use it in GitHub Desktop.
Use Telethon inside Celery worker
from celery import Celery
from celery.signals import worker_process_init
from telethon import TelegramClient
app = Celery('tasks', broker='redis://localhost:6379/2', backend='redis://localhost:6379/3')
api_id = 123 # set your api id
api_hash = 'qwe' # set your api hash
client = None
# init one client per worker process
@worker_process_init.connect
def setup_connections(**kwargs):
global client
client = TelegramClient('session_name', api_id, api_hash)
client.start()
@app.task
def test():
global client
return client.get_me().id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment