---
title: 函数：set_context
description: "| C函数原型 | aclError aclrtSetCurrentContext(aclrtContext context) |"
url: "https://www.hiascend.com/document/detail/zh/Atlas200IDKA2DeveloperKit/23.0.RC2/Application%20Development%20Guide/aadgp/aclpythondevg_01_0070.html"
---

# 函数：set_context

| C函数原型 | aclError aclrtSetCurrentContext(aclrtContext context) |
| --- | --- |
| Python函数 | ret = acl.rt.set\_context(context) |
| 函数功能 | 设置线程的Context，同步接口。 |
| 输入说明 | context：int，线程当前的Context对象指针地址。 |
| 返回值说明 | ret：int，错误码。 返回0表示成功。 返回其它值表示失败。 |
| 约束说明 | 支持以下场景： 如果在某线程（例如：thread1）中调用acl.rt.create\_context接口显式创建一个Context（例如：ctx1），则可以不调用acl.rt.set\_context接口指定该线程的Context，系统默认将ctx1作为thread1的Context。 如果没有调用acl.rt.create\_context接口显式创建Context，则系统将默认Context作为线程的Context，此时，不能通过acl.rt.destroy\_context接口来释放默认Context。 如果多次调用acl.rt.set\_context接口设置线程的Context，以最后一次为准。 若给线程设置的Context所对应的Device已经被复位，则不能将该Context设置为线程的Context，否则会导致业务异常。 推荐在某一线程中创建的Context，在该线程中使用。若在线程A中调用acl.rt.create\_context接口创建Context，在线程B中使用该Context，则需由用户自行保证两个线程中同一个Context下同一个Stream中任务执行的顺序。 |
| 参考资源 | 接口调用流程，参见多Device场景。 |
