context Package का उपयोग
In this page:
ctx, cancel := context.WithCancel(parent)
defer cancel()
ctx, cancel := context.WithTimeout(parent, duration)
select {
case <-ctx.Done():
// ctx.Err()
}
Creating a Context with a Timeout
context.WithTimeout एक context बनाता है जो दी गई duration बीतते ही अपने आप cancellation signal देता है, जो यह सीमित करने का standard तरीका है कि कोई operation कितनी देर चल सकता है।
उदाहरण: Creating a Context with a Timeout
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"context"
"fmt"
"time"
)
// main is where the program starts running
func main() {
// Short declaration: create ctx, cancel and infer the type
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond)
// defer runs this call when the surrounding function returns
defer cancel()
select {
case <-time.After(100 * time.Millisecond):
fmt.Println("work finished")
case <-ctx.Done():
fmt.Println("cancelled:", ctx.Err())
}
}
Login to try C/C++/Java/PHP code in the editor
Manual Cancellation with WithCancel
context.WithCancel एक context plus एक cancel function return करता है जो, call होने पर, तुरंत ctx.Done() देख रहे हर listener को signal देता है, सिर्फ एक timeout नहीं बल्कि किसी दूसरे event के जवाब में काम रोकने के लिए उपयोगी।
उदाहरण: Manual Cancellation with WithCancel
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"context"
"fmt"
)
// main is where the program starts running
func main() {
// Short declaration: create ctx, cancel and infer the type
ctx, cancel := context.WithCancel(context.Background())
// Start this call in a new goroutine (runs concurrently)
go func() {
// Print the values followed by a newline
fmt.Println("cancelling now")
cancel()
}()
<-ctx.Done()
// Print the values followed by a newline
fmt.Println("received cancellation:", ctx.Err())
}
Login to try C/C++/Java/PHP code in the editor
Passing Context to Functions
Idiomatic Go functions जिन्हें cancel या timeout होने की ज़रूरत पड़ सकती है अपने पहले parameter के रूप में एक context.Context accept करते हैं, callers को function की अपनी built-in timeout logic की ज़रूरत के बिना cancellation control करने देते हुए।
उदाहरण: Passing Context to Functions
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"context"
"fmt"
"time"
)
// Define the function doWork
func doWork(ctx context.Context) {
select {
case <-time.After(50 * time.Millisecond):
fmt.Println("work completed")
case <-ctx.Done():
fmt.Println("work cancelled:", ctx.Err())
}
}
// main is where the program starts running
func main() {
// Short declaration: create ctx, cancel and infer the type
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
// defer runs this call when the surrounding function returns
defer cancel()
doWork(ctx)
}
Login to try C/C++/Java/PHP code in the editor
- हर function के पहले parameter के रूप में explicitly इसे पास करने के बजाय किसी context को struct field के अंदर store करना, जैसा Go convention उम्मीद करता है।
- context.WithCancel/WithTimeout से returned cancel function call करना भूल जाना, context से जुड़े resources leak करते हुए।
- किसी लंबे समय तक चलने वाले goroutine के अंदर ctx.Done() को नज़रअंदाज़ करना, इसलिए context cancelled होने के बाद भी यह असल में कभी नहीं रुकता।
- context.Context API boundaries और goroutines में cancellation signals और deadlines ले जाता है।
- context.WithTimeout और context.WithCancel एक derived context plus एक cancel function बनाते हैं।
- किसी goroutine को cancellation notice करने और अपना काम जल्दी रोकने के लिए ctx.Done() पर select करना चाहिए।
- Convention: context.Context को explicitly किसी function के पहले parameter के रूप में पास किया जाता है, ctx नाम से।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: