-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAsyncSet.py
More file actions
executable file
·50 lines (39 loc) · 1.29 KB
/
AsyncSet.py
File metadata and controls
executable file
·50 lines (39 loc) · 1.29 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
import asyncio
class AsyncSet:
"""Thread/async-safe set."""
def __init__(self):
self._items = set()
self._lock = asyncio.Lock()
async def add(self, item):
"""Add item to set."""
async with self._lock:
self._items.add(item)
async def discard(self, item):
"""Remove item if present."""
async with self._lock:
self._items.discard(item)
async def remove(self, item):
"""Remove item, raise KeyError if not present."""
async with self._lock:
self._items.remove(item)
async def pop(self):
"""Remove and return arbitrary item."""
async with self._lock:
return self._items.pop()
async def clear(self):
"""Remove all items."""
async with self._lock:
self._items.clear()
async def copy(self):
"""Return a copy of the set."""
async with self._lock:
return self._items.copy()
def __contains__(self, item):
"""Check if item in set."""
return item in self._items
def __len__(self):
"""Get size of set."""
return len(self._items)
def __bool__(self):
"""Check if set is empty in pythonic way"""
return bool(self._items)