Repository: lightly-ai/lightly

Issue:
when pip version older than newest version, it calls API on every command rather than caching the information
When I use an older pip version, I see multiple lines of output like this when I run `lightly-magic`
```
...Python/3.8/lib/python/site-packages/lightly/api/version_checking.py:57: Warning: You are using lightly version 1.1.17. There is a newer version of the package available. For compatability reasons, please upgrade your current version: pip install lightly==1.1.18
  warnings.warn(Warning(warning))
```

Also tracking the connections it makes, it calls the API for **every** images I want to upload. So the pip does not cache the information that it is an outdated version. This is no bueno
  
  


Discussion:
I could reproduce it, it happens in the `lightly-embed` command. I found that the number of additional prints is equal to the number of `loader.num_workers`. It seems as if every worker does an import once it is initialised.

Resolving patch:
diff --git a/lightly/__init__.py b/lightly/__init__.py
--- a/lightly/__init__.py
+++ b/lightly/__init__.py
@@ -80,6 +80,7 @@
 __name__ = 'lightly'
 __version__ = '1.1.18'
 
+from multiprocessing import current_process
 
 try:
     # See (https://github.com/PyTorchLightning/pytorch-lightning)
@@ -118,14 +119,15 @@ def _is_prefetch_generator_available():
     from lightly import transforms
     from lightly import utils
 
+    if current_process().name == 'MainProcess':
+        # check for latest version
+        from lightly.api.version_checking import get_latest_version
+        from lightly.api.version_checking import version_compare
+        from lightly.api.version_checking import pretty_print_latest_version
 
-    # check for latest version
-    from lightly.api.version_checking import get_latest_version
-    from lightly.api.version_checking import version_compare
-    from lightly.api.version_checking import pretty_print_latest_version
+        latest_version = get_latest_version(__version__)
+        if latest_version is not None:
+            if version_compare(__version__, latest_version) < 0:
+                # local version is behind latest version
+                pretty_print_latest_version(latest_version)
 
-    latest_version = get_latest_version(__version__)
-    if latest_version is not None:
-        if version_compare(__version__, latest_version) < 0:
-            # local version is behind latest version
-            pretty_print_latest_version(latest_version)


Test patch:
diff --git a/tests/cli/test_cli_embed.py b/tests/cli/test_cli_embed.py
new file mode 100644
--- /dev/null
+++ b/tests/cli/test_cli_embed.py
@@ -0,0 +1,53 @@
+import os
+import re
+import sys
+import tempfile
+
+import torchvision
+from hydra.experimental import compose, initialize
+
+import lightly
+from tests.api_workflow.mocked_api_workflow_client import MockedApiWorkflowSetup, MockedApiWorkflowClient
+
+
+class TestCLIEmbed(MockedApiWorkflowSetup):
+
+    @classmethod
+    def setUpClass(cls) -> None:
+        sys.modules["lightly.cli.embed_cli"].ApiWorkflowClient = \
+            MockedApiWorkflowClient
+
+    def setUp(self):
+        MockedApiWorkflowSetup.setUp(self)
+        self.create_fake_dataset()
+        with initialize(config_path="../../lightly/cli/config", job_name="test_app"):
+            self.cfg = compose(config_name="config", overrides=[
+                "token='123'",
+                f"input_dir={self.folder_path}",
+                "trainer.max_epochs=0"
+            ])
+
+    def create_fake_dataset(self):
+        n_data = 16
+        self.dataset = torchvision.datasets.FakeData(size=n_data, image_size=(3, 32, 32))
+
+        self.folder_path = tempfile.mkdtemp()
+        sample_names = [f'img_{i}.jpg' for i in range(n_data)]
+        self.sample_names = sample_names
+        for sample_idx in range(n_data):
+            data = self.dataset[sample_idx]
+            path = os.path.join(self.folder_path, sample_names[sample_idx])
+            data[0].save(path)
+
+    def test_embed(self):
+        lightly.cli.embed_cli(self.cfg)
+
+    def tearDown(self) -> None:
+        for filename in ["embeddings.csv", "embeddings_sorted.csv"]:
+            try:
+                os.remove(filename)
+            except FileNotFoundError:
+                pass
+
+
+