nacos-sdk-python icon indicating copy to clipboard operation
nacos-sdk-python copied to clipboard

修复了python3.8及以上版本非windows系统不能监听配置的BUG

Open runzhi214 opened this issue 2 years ago • 5 comments
trafficstars

修复了ISSUE:https://github.com/nacos-group/nacos-sdk-python/issues/124 BUG在于:python3.8,3.9,3.10中,

  • 启动multiprocessing的时候会把process_mgr和callback_tread_pool也尝试序列话
  • 没有使用从mp库中引入的RLock(即pulling_lock和server_list_lock类型错误)。

runzhi214 avatar Dec 01 '22 10:12 runzhi214

@realJackSun

runzhi214 avatar Dec 01 '22 10:12 runzhi214

@runzhi214 用这个方式改了,似乎可以 ,我是MacOs的系统,但是报了一个新的错误,能辛苦解答下吗 image

Can't pickle local object 'MyTestCase.test_add_config_watch..test_cb'

chenhao26-nineteen avatar Dec 07 '22 08:12 chenhao26-nineteen

这个问题是由于test_client.py中使用了nested class和nested function导致的(因为pickle不允许),把他们重命名并放在测试类外部就可以通过了.

本地全部通过。其中,test_list_naming_instance_online这个测试方法需要单独测试,因为没有发送heartbeat的方法会马上死掉,在心跳结束之前可以看到测试通过。


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_fake_watcher(self):
        d = "test"
        g = "DEFAULT_GROUP"

        cache_key = "+".join([d, g, NAMESPACE])

        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(ShareFakeWatcher.content, "xxx")
        self.assertEqual(ShareFakeWatcher.count, 3)

        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(ShareFakeWatcher.content, "yyy")
        self.assertEqual(ShareFakeWatcher.count, 2)

        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(ShareFakeWatcher.content, "yyy")
        self.assertEqual(ShareFakeWatcher.count, 0)

        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(ShareFakeWatcher.content, "zzz")
        self.assertEqual(ShareFakeWatcher.count, 1)

        ShareFakeWatcher.count = 0
        client.notify_queue.put((cache_key, "not effective, md5 no changes", "md54"))
        time.sleep(1)
        self.assertEqual(ShareFakeWatcher.content, "zzz")
        self.assertEqual(ShareFakeWatcher.count, 0)

    def test_long_pulling(self):
        client2 = nacos.NacosClient(SERVER_ADDRESSES, username=USERNAME, password=PASSWORD)
        d = "test1_pulling"
        g = "Group1"
        g2 = "Group2"

        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_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(ShareLongPulling.content, "test")
        client2.publish_config(d, g2, u"test2中文")
        time.sleep(1)
        self.assertEqual(ShareLongPulling.content, u"test2中文")
截屏2022-12-07 17 28 38 截屏2022-12-07 17 30 16

runzhi214 avatar Dec 07 '22 09:12 runzhi214

python一旦涉及到pickling和multiprocessing移植性就变得很差啊。。。

shouldsee avatar Dec 19 '22 06:12 shouldsee

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Oct 23 '23 13:10 CLAassistant