title: Save an object using Celery and Gino
body: <p>I have the following pipeline</p>
  <p>Event model (based on Gino 'db' object):</p>
  <pre><code>class Event(db.Model):
      __tablename__ = &quot;events&quot;
  
      id = db.Column(db.BigInteger(), primary_key=True)
      request_timestamp = db.Column(db.DateTime(), nullable=False)
      service = db.Column(db.String(), nullable=False)
      url = db.Column(db.String(), nullable=False)
      status_code = db.Column(db.Integer(), nullable=False)
      response_time = db.Column(db.DateTime(), nullable=False)
  </code></pre>
  <p>FastApi app has a POST view with call to celery task to pass the data of the Event object:</p>
  <pre><code>@router.post(&quot;/events/add&quot;)
  async def add_event(event: EventModel):
      event_data = {'request_timestamp': event.request_timestamp.replace(tzinfo=None),
                    'service': event.service,
                    'url': event.url,
                    'status_code': event.status_code,
                    'response_time': event.response_time.replace(tzinfo=None)
                    }
  
      task = celery_app.send_task(&quot;monitoring_service.src.monitoring_service.worker.celery_worker.add_to_db&quot;,
                                  kwargs=event_data)
  
      return JSONResponse(content=&quot;Event recorded successfully&quot;, status_code=200)
  </code></pre>
  <p>And celery worker which must save an Event object to database:</p>
  <pre><code>@celery_app.task(acks_late=True)
  async def add_to_db(request_timestamp, service, url, status_code, response_time):
      event = await Event.create(
          request_timestamp=datetime.strptime(request_timestamp, '%Y-%m-%dT%H:%M:%S.%f'),
          service=service,
          url=url,
          status_code=status_code,
          response_time=datetime.strptime(response_time, '%Y-%m-%dT%H:%M:%S.%f'),
      )
  
      return {&quot;status&quot;: True}
  </code></pre>
  <p>Now I'm receiving an error during object saving process:</p>
  <pre><code>Traceback (most recent call last):
  
  File &quot;/usr/local/lib/python3.8/site-packages/celery/app/trace.py&quot;, line 479, in trace_task
  
  mark_as_done(
  
  File &quot;/usr/local/lib/python3.8/site-packages/celery/backends/base.py&quot;, line 158, in mark_as_done
  
  self.store_result(task_id, result, state, request=request)
  
  File &quot;/usr/local/lib/python3.8/site-packages/celery/backends/base.py&quot;, line 442, in store_result
  
  self._store_result(task_id, result, state, traceback,
  
  File &quot;/usr/local/lib/python3.8/site-packages/celery/backends/database/__init__.py&quot;, line 51, in _inner
  
  return fun(*args, **kwargs)
  
  File &quot;/usr/local/lib/python3.8/site-packages/celery/backends/database/__init__.py&quot;, line 130, in _store_result
  
  session.commit()
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 1042, in commit
  
  self.transaction.commit()
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 504, in commit
  
  self._prepare_impl()
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 483, in _prepare_impl
  
  self.session.flush()
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 2523, in flush
  
  self._flush(objects)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 2664, in _flush
  
  transaction.rollback(_capture_exception=True)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py&quot;, line 68, in __exit__
  
  compat.raise_(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py&quot;, line 178, in raise_
  
  raise exception
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 2624, in _flush
  
  flush_context.execute()
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py&quot;, line 422, in execute
  
  rec.execute(self)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py&quot;, line 586, in execute
  
  persistence.save_obj(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py&quot;, line 230, in save_obj
  
  _emit_update_statements(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py&quot;, line 994, in _emit_update_statements
  
  c = cached_connections[connection].execute(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py&quot;, line 1014, in execute
  
  return meth(self, multiparams, params)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/elements.py&quot;, line 298, in _execute_on_connection
  
  return connection._execute_clauseelement(self, multiparams, params)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py&quot;, line 1127, in _execute_clauseelement
  
  ret = self._execute_context(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py&quot;, line 1207, in _execute_context
  
  self._handle_dbapi_exception(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py&quot;, line 1511, in _handle_dbapi_exception
  
  util.raise_(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py&quot;, line 178, in raise_
  
  raise exception
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py&quot;, line 1205, in _execute_context
  
  context = constructor(dialect, self, conn, *args)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py&quot;, line 858, in _init_compiled
  
  param = dict(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py&quot;, line 861, in &lt;genexpr&gt;
  
  processors[key](compiled_params[key])
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/sqltypes.py&quot;, line 1689, in process
  
  value = dumps(value, protocol)
  
  sqlalchemy.exc.SQLAlchemyError: (builtins.TypeError) cannot pickle 'coroutine' object
  
  
  warn(RuntimeWarning(
  
  [2021-02-19 10:19:39,010: ERROR/ForkPoolWorker-1] Task monitoring_service.src.monitoring_service.worker.celery_worker.add_to_db[994ae754-f216-4fa9-b502-5fbb0221011c] raised unexpected: SQLAlchemyError(&quot;(builtins.TypeError) cannot pickle 'coroutine' object&quot;)
  
  Traceback (most recent call last):
  
  File &quot;/usr/local/lib/python3.8/site-packages/celery/app/trace.py&quot;, line 479, in trace_task
  
  mark_as_done(
  
  File &quot;/usr/local/lib/python3.8/site-packages/celery/backends/base.py&quot;, line 158, in mark_as_done
  
  self.store_result(task_id, result, state, request=request)
  
  File &quot;/usr/local/lib/python3.8/site-packages/celery/backends/base.py&quot;, line 442, in store_result
  
  self._store_result(task_id, result, state, traceback,
  
  File &quot;/usr/local/lib/python3.8/site-packages/celery/backends/database/__init__.py&quot;, line 51, in _inner
  
  return fun(*args, **kwargs)
  
  File &quot;/usr/local/lib/python3.8/site-packages/celery/backends/database/__init__.py&quot;, line 130, in _store_result
  
  session.commit()
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 1042, in commit
  
  self.transaction.commit()
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 504, in commit
  
  self._prepare_impl()
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 483, in _prepare_impl
  
  self.session.flush()
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 2523, in flush
  
  self._flush(objects)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 2664, in _flush
  
  transaction.rollback(_capture_exception=True)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py&quot;, line 68, in __exit__
  
  compat.raise_(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py&quot;, line 178, in raise_
  
  raise exception
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py&quot;, line 2624, in _flush
  
  flush_context.execute()
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py&quot;, line 422, in execute
  
  rec.execute(self)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py&quot;, line 586, in execute
  
  persistence.save_obj(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py&quot;, line 230, in save_obj
  
  _emit_update_statements(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py&quot;, line 994, in _emit_update_statements
  
  c = cached_connections[connection].execute(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py&quot;, line 1014, in execute
  
  return meth(self, multiparams, params)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/elements.py&quot;, line 298, in _execute_on_connection
  
  return connection._execute_clauseelement(self, multiparams, params)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py&quot;, line 1127, in _execute_clauseelement
  
  ret = self._execute_context(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py&quot;, line 1207, in _execute_context
  
  self._handle_dbapi_exception(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py&quot;, line 1511, in _handle_dbapi_exception
  
  util.raise_(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py&quot;, line 178, in raise_
  
  raise exception
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py&quot;, line 1205, in _execute_context
  
  context = constructor(dialect, self, conn, *args)
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py&quot;, line 858, in _init_compiled
  
  param = dict(
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py&quot;, line 861, in &lt;genexpr&gt;
  
  processors[key](compiled_params[key])
  
  File &quot;/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/sqltypes.py&quot;, line 1689, in process
  
  value = dumps(value, protocol)
  
  sqlalchemy.exc.SQLAlchemyError: (builtins.TypeError) cannot pickle 'coroutine' object
  </code></pre>
  <p>If I call <code>event = await Event.create(...)</code> inside a view it is successfull and saves data to db.
  If I delete 'async/await' from Celery worker, this error doesn't show up, Celery task has 'SUCCESS' state  but database is empty.
  Can't really understand what is not right.</p>