From 05e30aacf35207fdb31f7ac528d227d21846d545 Mon Sep 17 00:00:00 2001 From: runzhi214 Date: Thu, 1 Dec 2022 18:28:54 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dpy3.8+(=E5=90=AB3.9,3.10?= =?UTF-8?q?=EF=BC=89=E7=89=88=E6=9C=AC=E4=B8=AD=EF=BC=8C=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BC=9A=E6=8A=A5=E9=94=99Not=20Serializable?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=88=E9=9D=9Ewindows=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E4=B8=8B=EF=BC=8CRLock=E6=94=B9=E7=94=A8mulitprocessi?= =?UTF-8?q?ng=E5=BA=93=E7=9A=84RLock.=20=E5=90=8C=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E6=8A=8Acallback=5Ftread=5Fpool=E5=92=8Cprocess=5Fmgr=E6=A0=87?= =?UTF-8?q?=E8=AE=B0=E4=B8=BAtransient,=E5=9B=A0=E4=B8=BA=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=BA=8F=E5=88=97=E5=8C=96=E7=9A=84=E5=BF=85=E8=A6=81?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nacos/client.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/nacos/client.py b/nacos/client.py index ff527ad..157f8c6 100644 --- a/nacos/client.py +++ b/nacos/client.py @@ -15,6 +15,7 @@ from multiprocessing import Process, Manager, Queue, pool from threading import RLock, Thread +from multiprocessing import RLock as PRLock try: # python3.6 @@ -251,13 +252,19 @@ def __init__(self, server_addresses, endpoint=None, namespace=None, ak=None, sk= self.username = username self.password = password - self.server_list_lock = RLock() + if platform.system() == "windows": + self.server_list_lock = RLock() + else: + self.server_list_lock = PRLock() self.server_offset = 0 self.watcher_mapping = dict() self.subscribed_local_manager = SubscribedLocalManager() self.subscribe_timer_manager = NacosTimerManager() - self.pulling_lock = RLock() + if platform.system() == "windows": + self.pulling_lock = RLock() + else: + self.pulling_lock = PRLock() self.puller_mapping = None self.notify_queue = None self.callback_tread_pool = None @@ -1163,6 +1170,17 @@ def stop_subscribe(self): """ self.subscribe_timer_manager.stop() + def __getstate__(self): + self_dict = self.__dict__.copy() + # pool object cannot be passed and there is no need to + del self_dict['callback_tread_pool'] + # weak-ref object cannot be pickled and there is no need to + del self_dict['process_mgr'] + return self_dict + + def __setstate__(self, state): + self.__dict__.update(state) + if DEBUG: NacosClient.set_debugging() From 63d37a77d154574d9cb8a0762441d70d72112a90 Mon Sep 17 00:00:00 2001 From: runzhi214 Date: Thu, 1 Dec 2022 18:31:26 +0800 Subject: [PATCH 2/4] =?UTF-8?q?Revert=20"=E4=BF=AE=E5=A4=8Dpy3.8+(?= =?UTF-8?q?=E5=90=AB3.9,3.10=EF=BC=89=E7=89=88=E6=9C=AC=E4=B8=AD=EF=BC=8C?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E9=85=8D=E7=BD=AE=E4=BC=9A=E6=8A=A5=E9=94=99?= =?UTF-8?q?Not=20Serializable=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=88=E9=9D=9E?= =?UTF-8?q?windows=E7=8E=AF=E5=A2=83=E4=B8=8B=EF=BC=8CRLock=E6=94=B9?= =?UTF-8?q?=E7=94=A8mulitprocessing=E5=BA=93=E7=9A=84RLock.=20=E5=90=8C?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E6=8A=8Acallback=5Ftread=5Fpool=E5=92=8Cproc?= =?UTF-8?q?ess=5Fmgr=E6=A0=87=E8=AE=B0=E4=B8=BAtransient,=E5=9B=A0?= =?UTF-8?q?=E4=B8=BA=E6=B2=A1=E6=9C=89=E5=BA=8F=E5=88=97=E5=8C=96=E7=9A=84?= =?UTF-8?q?=E5=BF=85=E8=A6=81=EF=BC=89"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 05e30aacf35207fdb31f7ac528d227d21846d545. --- nacos/client.py | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/nacos/client.py b/nacos/client.py index 157f8c6..ff527ad 100644 --- a/nacos/client.py +++ b/nacos/client.py @@ -15,7 +15,6 @@ from multiprocessing import Process, Manager, Queue, pool from threading import RLock, Thread -from multiprocessing import RLock as PRLock try: # python3.6 @@ -252,19 +251,13 @@ def __init__(self, server_addresses, endpoint=None, namespace=None, ak=None, sk= self.username = username self.password = password - if platform.system() == "windows": - self.server_list_lock = RLock() - else: - self.server_list_lock = PRLock() + self.server_list_lock = RLock() self.server_offset = 0 self.watcher_mapping = dict() self.subscribed_local_manager = SubscribedLocalManager() self.subscribe_timer_manager = NacosTimerManager() - if platform.system() == "windows": - self.pulling_lock = RLock() - else: - self.pulling_lock = PRLock() + self.pulling_lock = RLock() self.puller_mapping = None self.notify_queue = None self.callback_tread_pool = None @@ -1170,17 +1163,6 @@ def stop_subscribe(self): """ self.subscribe_timer_manager.stop() - def __getstate__(self): - self_dict = self.__dict__.copy() - # pool object cannot be passed and there is no need to - del self_dict['callback_tread_pool'] - # weak-ref object cannot be pickled and there is no need to - del self_dict['process_mgr'] - return self_dict - - def __setstate__(self, state): - self.__dict__.update(state) - if DEBUG: NacosClient.set_debugging() From 448a76dadeccdb6cff123825b9aec5d8657f028b Mon Sep 17 00:00:00 2001 From: runzhi214 Date: Thu, 1 Dec 2022 18:33:44 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86py3.8+(=E5=90=AB?= =?UTF-8?q?3.8,3.9,3.10)=E7=9A=84=E7=9B=91=E5=90=AC=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=97=B6=E6=8A=A5=E9=94=99not=20serializable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在非windows环境改用mp库中的RLock,同时将不需要序列话的process_mgr和callback_tread_pool标记为transient FIX TO ISSUE https://github.com/nacos-group/nacos-sdk-python/issues/124 --- nacos/client.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/nacos/client.py b/nacos/client.py index ff527ad..e8d822a 100644 --- a/nacos/client.py +++ b/nacos/client.py @@ -15,6 +15,7 @@ from multiprocessing import Process, Manager, Queue, pool from threading import RLock, Thread +from multiprocessing import RLock as PRLock try: # python3.6 @@ -251,13 +252,19 @@ def __init__(self, server_addresses, endpoint=None, namespace=None, ak=None, sk= self.username = username self.password = password - self.server_list_lock = RLock() + if platform.system() == "windows": + self.server_list_lock = RLock() + else: + self.server_list_lock = PRLock() self.server_offset = 0 self.watcher_mapping = dict() self.subscribed_local_manager = SubscribedLocalManager() self.subscribe_timer_manager = NacosTimerManager() - self.pulling_lock = RLock() + if platform.system() == "windows": + self.pulling_lock = RLock() + else: + self.pulling_lock = PRLock() self.puller_mapping = None self.notify_queue = None self.callback_tread_pool = None @@ -1163,6 +1170,17 @@ def stop_subscribe(self): """ self.subscribe_timer_manager.stop() + def __getstate__(self): + self_dict = self.__dict__.copy() + # pool object cannot be passed + del self_dict['callback_tread_pool'] + # weak-ref object cannot be pickled + del self_dict['process_mgr'] + return self_dict + + def __setstate__(self, state): + self.__dict__.update(state) + if DEBUG: NacosClient.set_debugging() From 62ce868c374d15941881ddf43c38f8808f5407f0 Mon Sep 17 00:00:00 2001 From: runzhi214 Date: Wed, 7 Dec 2022 17:25:07 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=8F=AF=E7=94=A8?= =?UTF-8?q?=E7=9A=84test=E7=B1=BB=EF=BC=9A=E4=BF=AE=E5=A4=8D=E4=BA=86?= =?UTF-8?q?=E4=BD=BF=E7=94=A8Nested=20Class=20=E5=92=8C=20Nested=20Functio?= =?UTF-8?q?n=E4=B8=8D=E8=83=BD=E8=A2=ABpickle=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82=E6=B5=8B=E8=AF=95=E5=85=A8=E9=83=A8=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E3=80=82test=5Flist=5Fnaming=5Finstance=5Fonline=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E9=85=8D=E5=90=88=E6=9C=AC=E5=9C=B0=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=EF=BC=88=E5=AE=9E=E9=99=85=E5=B7=B2=E7=BB=8F=E6=88=90=E5=8A=9F?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FIX TO ISSUE https://github.com/nacos-group/nacos-sdk-python/issues/124 Info to https://github.com/nacos-group/nacos-sdk-python/pull/125 --- test/client_test.py | 94 +++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/test/client_test.py b/test/client_test.py index 3d25daa..8dc5fbe 100644 --- a/test/client_test.py +++ b/test/client_test.py @@ -11,11 +11,10 @@ import time import shutil -SERVER_1 = "100.69.207.65" +SERVER_1 = "192.168.0.104" SERVER_ADDRESSES = "%s:8848, 100.69.207.66:8848" % SERVER_1 -SERVER_ADDRESSES = "127.0.0.1:8848" -NAMESPACE = "6cface1f-2f1b-4744-a59d-fd818b91a799" -NAMESPACE = "" +SERVER_ADDRESSES = "192.168.0.104:8848" +NAMESPACE = "28162559-3344-4bec-adc2-cc7dc8fa13f2" # Set the following values if authentication mode is enabled on the server USERNAME = None @@ -25,6 +24,27 @@ # Set the following option if http requests need through by proxy # client.set_options(proxies={"http":"192.168.56.1:809"}) + +class ShareFakeWatcher: + content = None + count = 0 + + +def test_cb_fake_watcher(args): + print(args) + ShareFakeWatcher.count += 1 + ShareFakeWatcher.content = args["content"] + + +class ShareLongPulling: + content = None + + +def cb_long_pulling(x): + ShareLongPulling.content = x["content"] + print(ShareLongPulling.content) + + class TestClient(unittest.TestCase): def test_get_server(self): self.assertEqual(client.get_server(), (SERVER_1, 8848)) @@ -57,53 +77,44 @@ def test_fake_watcher(self): d = "test" g = "DEFAULT_GROUP" - class Share: - content = None - count = 0 - cache_key = "+".join([d, g, NAMESPACE]) - def test_cb(args): - print(args) - Share.count += 1 - Share.content = args["content"] - - client.add_config_watcher(d, g, test_cb) - client.add_config_watcher(d, g, test_cb) - client.add_config_watcher(d, g, test_cb) + client.add_config_watcher(d, g, test_cb_fake_watcher) + client.add_config_watcher(d, g, test_cb_fake_watcher) + client.add_config_watcher(d, g, test_cb_fake_watcher) time.sleep(1) client.notify_queue.put((cache_key, "xxx", "md51")) time.sleep(1) - self.assertEqual(Share.content, "xxx") - self.assertEqual(Share.count, 3) + self.assertEqual(ShareFakeWatcher.content, "xxx") + self.assertEqual(ShareFakeWatcher.count, 3) - client.remove_config_watcher(d, g, test_cb) - Share.count = 0 + client.remove_config_watcher(d, g, test_cb_fake_watcher) + ShareFakeWatcher.count = 0 client.notify_queue.put((cache_key, "yyy", "md52")) time.sleep(1) - self.assertEqual(Share.content, "yyy") - self.assertEqual(Share.count, 2) + self.assertEqual(ShareFakeWatcher.content, "yyy") + self.assertEqual(ShareFakeWatcher.count, 2) - client.remove_config_watcher(d, g, test_cb, True) - Share.count = 0 + client.remove_config_watcher(d, g, test_cb_fake_watcher, True) + ShareFakeWatcher.count = 0 client.notify_queue.put((cache_key, "not effective, no watchers", "md53")) time.sleep(1) - self.assertEqual(Share.content, "yyy") - self.assertEqual(Share.count, 0) + self.assertEqual(ShareFakeWatcher.content, "yyy") + self.assertEqual(ShareFakeWatcher.count, 0) - Share.count = 0 - client.add_config_watcher(d, g, test_cb) + ShareFakeWatcher.count = 0 + client.add_config_watcher(d, g, test_cb_fake_watcher) time.sleep(1) client.notify_queue.put((cache_key, "zzz", "md54")) time.sleep(1) - self.assertEqual(Share.content, "zzz") - self.assertEqual(Share.count, 1) + self.assertEqual(ShareFakeWatcher.content, "zzz") + self.assertEqual(ShareFakeWatcher.count, 1) - Share.count = 0 + ShareFakeWatcher.count = 0 client.notify_queue.put((cache_key, "not effective, md5 no changes", "md54")) time.sleep(1) - self.assertEqual(Share.content, "zzz") - self.assertEqual(Share.count, 0) + self.assertEqual(ShareFakeWatcher.content, "zzz") + self.assertEqual(ShareFakeWatcher.count, 0) def test_long_pulling(self): client2 = nacos.NacosClient(SERVER_ADDRESSES, username=USERNAME, password=PASSWORD) @@ -111,27 +122,20 @@ def test_long_pulling(self): g = "Group1" g2 = "Group2" - class Share: - content = None - - def cb(x): - Share.content = x["content"] - print(Share.content) - client2.publish_config(d, g, "test2") client2.publish_config(d, g2, "test2") time.sleep(0.5) # test common - client2.add_config_watcher(d, g, cb) - client2.add_config_watcher(d, g2, cb) + client2.add_config_watcher(d, g, cb_long_pulling) + client2.add_config_watcher(d, g2, cb_long_pulling) time.sleep(0.5) client2.publish_config(d, g, "test") client2.publish_config(d, g2, "test") time.sleep(1) - self.assertEqual(Share.content, "test") + self.assertEqual(ShareLongPulling.content, "test") client2.publish_config(d, g2, u"test2中文") time.sleep(1) - self.assertEqual(Share.content, u"test2中文") + self.assertEqual(ShareLongPulling.content, u"test2中文") def test_get_from_failover(self): d = "test_fo" @@ -179,7 +183,7 @@ def test_list_naming_instance_offline(self): self.assertEqual(len(client.list_naming_instance("test.service")["hosts"]), 0) def test_list_naming_instance_online(self): - client.add_naming_instance("test.service", "1.0.0.1", 8080, "testCluster2", 0.1, "{}", True, True) + client.add_naming_instance("test.service", "127.0.0.1", 9876, "testCluster2", 0.1, "{}", True, True) self.assertEqual(len(client.list_naming_instance("test.service")["hosts"]), 1) def test_get_naming_instance(self):