Skip to content

Commit 89c039f

Browse files
committed
* Removed sync waiting from downlink models.
* Added cookbook example for join map lane.
1 parent ff0cbdf commit 89c039f

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

examples/cookbook/join_map_lane.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2015-2020 SWIM.AI inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# Licensed under the Apache License, Version 2.0 (the "License");
16+
# you may not use this file except in compliance with the License.
17+
# You may obtain a copy of the License at
18+
#
19+
# http://www.apache.org/licenses/LICENSE-2.0
20+
#
21+
# Unless required by applicable law or agreed to in writing, software
22+
# distributed under the License is distributed on an "AS IS" BASIS,
23+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24+
# See the License for the specific language governing permissions and
25+
# limitations under the License.
26+
#
27+
# Licensed under the Apache License, Version 2.0 (the "License");
28+
# you may not use this file except in compliance with the License.
29+
# You may obtain a copy of the License at
30+
#
31+
# http://www.apache.org/licenses/LICENSE-2.0
32+
#
33+
# Unless required by applicable law or agreed to in writing, software
34+
# distributed under the License is distributed on an "AS IS" BASIS,
35+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36+
# See the License for the specific language governing permissions and
37+
# limitations under the License.
38+
#
39+
# Licensed under the Apache License, Version 2.0 (the "License");
40+
# you may not use this file except in compliance with the License.
41+
# You may obtain a copy of the License at
42+
#
43+
# http://www.apache.org/licenses/LICENSE-2.0
44+
#
45+
# Unless required by applicable law or agreed to in writing, software
46+
# distributed under the License is distributed on an "AS IS" BASIS,
47+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
48+
# See the License for the specific language governing permissions and
49+
# limitations under the License.
50+
import time
51+
from swimai import SwimClient
52+
53+
THRESHOLD = 1000
54+
55+
56+
async def custom_did_update(key, new_value, old_value):
57+
if new_value > THRESHOLD:
58+
print(f'{key} has {new_value} residents')
59+
60+
61+
if __name__ == '__main__':
62+
with SwimClient() as swim_client:
63+
host_uri = 'warp://localhost:53556'
64+
node_uri = '/join/state/all'
65+
lane_uri = 'join'
66+
67+
map_downlink = swim_client.downlink_map()
68+
map_downlink.set_host_uri(host_uri)
69+
map_downlink.set_node_uri(node_uri)
70+
map_downlink.set_lane_uri(lane_uri)
71+
map_downlink.did_update(custom_did_update)
72+
map_downlink.open()
73+
74+
time.sleep(2)
75+
76+
print('Stopping the client in 2 seconds')
77+
time.sleep(2)

swimai/client/_downlinks/_downlinks.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,10 @@ async def _send_message(self, message: '_Envelope') -> None:
378378

379379
async def _get_value(self) -> Any:
380380
"""
381-
Get the value of the downlink after it has been synced.
381+
Get the value of the downlink.
382382
383383
:return: - The current value of the downlink.
384384
"""
385-
await self._synced.wait()
386385
return self._value
387386

388387
async def __set_value(self, message: '_Envelope') -> None:
@@ -529,7 +528,6 @@ async def _get_value(self, key) -> Any:
529528
:param key - The key of the entry.
530529
:return: - The current value of the downlink.
531530
"""
532-
await self._synced.wait()
533531
return self._map.get(key, (Value.absent(), Value.absent()))[1]
534532

535533
async def _get_values(self) -> list:
@@ -538,7 +536,6 @@ async def _get_values(self) -> list:
538536
539537
:return: - A list with all the values from the downlink map.
540538
"""
541-
await self._synced.wait()
542539
return list(self._map.values())
543540

544541
async def __receive_update(self, message: '_Envelope') -> None:

0 commit comments

Comments
 (0)