# How to cancel activity

**URL:** <https://community.temporal.io/t/how-to-cancel-activity/7911>\
**Category:** Community Support\
**Tags:** python-sdk\
**Created:** [April 13, 2023, 8:43pm UTC](https://community.temporal.io/t/how-to-cancel-activity/7911 "2023-04-13T20:43:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isptn](https://avatars.discourse-cdn.com/v4/letter/i/e36b37/32.png) [@Isptn](https://community.temporal.io/u/Isptn)\
**Post date:** [April 13, 2023, 8:43pm UTC](https://community.temporal.io/t/how-to-cancel-activity/7911/1 "2023-04-13T20:43:46Z")

</div>

Is there a way to cancel an activity after some time?  
The thing is activity is waiting for X hours if it was not canceled it does the work.  
The signal came into my mind, but maybe there is a more elegant way to do so.

---

<div class="post-metadata">

**Author:** ![Chad\_Retz](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/chad_retz/32/1396_2.png) [@Chad\_Retz](https://community.temporal.io/u/Chad_Retz)\
**Post date:** [April 13, 2023, 9:01pm UTC](https://community.temporal.io/t/how-to-cancel-activity/7911/2 "2023-04-13T21:01:38Z")

</div>

Assuming the activity is properly heartbeating and you have a reasonable heartbeat timeout, you cancel a started activity task just like you would any other asyncio task.

So you should be able to `task = workflow.start_activity(...)` + `task.cancel()` or `task = asyncio.create_task(workflow.execute_activity(...))` + `task.cancel()` (they are essentially the same thing). This also means asyncio cancellation propagates through activity tasks (e.g. workflow cancellation cancels awaited activities, new 3.11 task groups, etc).

Just don’t forget the heartbeat timeout and to regularly heartbeat from the activity. Also, you may want to look at the `cancellation_type` option when starting the activity because it affects how activity cancellation is handled with regards to waiting on the cancellation to take effect (by default cancellation is issued but activity handling of the cancellation is not waited on, i.e. “try cancel”).

---

<div class="post-metadata">

**Author:** ![Patrick\_Rachford](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/patrick_rachford/32/1996_2.png) [@Patrick\_Rachford](https://community.temporal.io/u/Patrick_Rachford)\
**Post date:** [April 14, 2023, 4:51pm UTC](https://community.temporal.io/t/how-to-cancel-activity/7911/3 "2023-04-14T16:51:40Z")

</div>

Adding to what Chad said, there are docs that show [how to cancel an Activity](https://docs.temporal.io/application-development/features?lang=python#cancel-an-activity).
