Multithreading, Multistreaming Scenario
After APIs are called, add an exception handling branch, and record error logs and warning logs. The sample code of key steps is as follows, which is for reference only. Do not directly copy and run the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import acl # ...... def run_thread(): device_id = 0 # Specify a compute device. ret = acl.rt.set_device(device_id) # Explicitly create a stream. stream, ret = acl.rt.create_stream() # Call the task triggering API. # .... # Destroy allocations. ret = acl.rt.destroy_stream(stream) # Create two threads. Each thread internally creates a stream. thread_id1, ret = acl.util.start_thread(run_thread, None) thread_id2, ret = acl.util.start_thread(run_thread, None) # Explicitly call the stop_thread function to ensure that the threads are ended. ret = acl.util.stop_thread(thread_id1) ret = acl.util.stop_thread(thread_id2) |
Parent topic: Stream Management