-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_python.go
More file actions
81 lines (67 loc) · 1.78 KB
/
_python.go
File metadata and controls
81 lines (67 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package masktunnel
import (
"context"
"sync"
"time"
)
// This file provides small, gopy-friendly helpers for Python bindings.
// ========== Context helpers ==========
var (
globalCtx context.Context
globalCancel context.CancelFunc
globalOnce sync.Once
)
// Background returns a process-wide background context.
func Background() context.Context {
globalOnce.Do(func() {
globalCtx, globalCancel = context.WithCancel(context.Background())
})
return globalCtx
}
// ContextWithCancel wraps a context and its cancel function.
type ContextWithCancel struct {
ctx context.Context
cancel context.CancelFunc
}
// NewContextWithCancel creates a new cancellable context.
func NewContextWithCancel() *ContextWithCancel {
ctx, cancel := context.WithCancel(context.Background())
return &ContextWithCancel{ctx: ctx, cancel: cancel}
}
// Cancel cancels the underlying context.
func (c *ContextWithCancel) Cancel() {
if c != nil && c.cancel != nil {
c.cancel()
}
}
// Context returns the underlying context.
func (c *ContextWithCancel) Context() context.Context {
if c == nil {
return context.Background()
}
return c.ctx
}
// CancelGlobalContext cancels the global background context.
func CancelGlobalContext() {
if globalCancel != nil {
globalCancel()
}
}
// NewContext returns a new background-derived context.
func NewContext() context.Context {
ctx, _ := context.WithCancel(context.Background())
return ctx
}
// ========== Time constants ==========
var (
Nanosecond = time.Nanosecond
Microsecond = time.Microsecond
Millisecond = time.Millisecond
Second = time.Second
Minute = time.Minute
Hour = time.Hour
)
// ParseDuration parses a duration string (e.g. "300ms", "2h45m").
func ParseDuration(s string) (time.Duration, error) {
return time.ParseDuration(s)
}