question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pickle error when running tests for ChannelsLiveServerTestCase for Windows

See original GitHub issue

I was following a tutorial, and I’ve been getting AttributeError: Can't pickle local object 'DaphneProcess.__init__.<locals>.<lambda>'.

I’ve done further research into this and it turns out to be a problem with Python’s multiprocessing library with lambdas on Windows. There’s a solution on Stack Overflow but changing code in the standard library is not very maintainable.

I went back to the testing code and stripped away all the test cases and just put one test case. I think this is a problem during the server setup. Here’s the code:

from channels.testing import ChannelsLiveServerTestCase


class ChatTests(ChannelsLiveServerTestCase):
    serve_static = True

    def test_1(self):
        self.assertTrue(True)

Are there any other alternatives or solutions to this?

Traceback:

ERROR: test_1 (chat.tests.ChatTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\ederi\Documents\projects\django_chat_project\venv\lib\site-packages\django\test\testcases.py", line 270, in _setup_and_call
    self._pre_setup()
  File "C:\Users\ederi\Documents\projects\django_chat_project\venv\lib\site-packages\channels\testing\live.py", line 52, in _pre_setup
    self._server_process.start()
  File "C:\Users\ederi\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\ederi\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\ederi\AppData\Local\Programs\Python\Python37\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\ederi\AppData\Local\Programs\Python\Python37\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\ederi\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'DaphneProcess.__init__.<locals>.<lambda>'

----------------------------------------------------------------------
Ran 0 tests in 0.024s

FAILED (errors=1)
Destroying test database for alias 'default'...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\ederi\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Users\ederi\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 115, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input

OS: Windows 10 Using: Django 3.1, channels 2.4.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
deepiocommented, Jun 13, 2021

It seems like this is no small issue, what do you suggest for the time being?

OS: macOS BigSur 11.2.3 Python: 3.9.1 Channels: 3.0.3 Django: 3.2.3

I was following a tutorial, and I’ve been getting AttributeError: Can't pickle local object 'DaphneProcess.__init__.<locals>.<lambda>'.

I’ve done further research into this and it turns out to be a problem with Python’s multiprocessing library with lambdas on Windows. There’s a solution on Stack Overflow but changing code in the standard library is not very maintainable.

I went back to the testing code and stripped away all the test cases and just put one test case. I think this is a problem during the server setup. Here’s the code:

from channels.testing import ChannelsLiveServerTestCase


class ChatTests(ChannelsLiveServerTestCase):
    serve_static = True

    def test_1(self):
        self.assertTrue(True)
Traceback (most recent call last):
  File "/Users/deepio/Documents/GitHub/channels_tut/env/lib/python3.9/site-packages/django/test/testcases.py", line 272, in _setup_and_call
    self._pre_setup()
  File "/Users/deepio/Documents/GitHub/channels_tut/env/lib/python3.9/site-packages/channels/testing/live.py", line 52, in _pre_setup
    self._server_process.start()
  File "/Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/multiprocessing/context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "/Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/multiprocessing/context.py", line 284, in _Popen
    return Popen(process_obj)
  File "/Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 47, in _launch
    reduction.dump(process_obj, fp)
  File "/Users/deepio/.pyenv/versions/3.9.1/lib/python3.9/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'convert_exception_to_response.<locals>.inner'
0reactions
carltongibsoncommented, Aug 21, 2022

I’m going to close this as a duplicate of #1207 to focus there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pickle error when running tests for ChannelsLiveServerTestCase for ...
I've done further research into this and it turns out to be a problem with Python's multiprocessing library with lambdas on Windows. There's...
Read more >
Getting a "Can't pickle local object 'LiveServerTestCase'" error ...
I'm developing on Windows, so from what I could tell I'm kinda out of ... I tried running the tests there and it...
Read more >
cant get selenium to work i want to test my django channels ...
im trying to test my app and even if i do the most basic things my selenium refuses to work all i get...
Read more >
Testing — Channels 4.0.0 documentation
Testing ¶. Testing Channels consumers is a little trickier than testing normal Django views due to their underlying asynchronous nature. To help with...
Read more >
django-channels-jwt-auth-middleware - PyPI
I have tried testing the middleware through ChannelsLiveServerTestCase , but currently this does not run due to pickle error in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found