강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của rhyou104706
rhyou104706

câu hỏi đã được viết

Hướng dẫn hoàn chỉnh về phát triển dịch vụ web Python/Django với React

vỏ django

주피터 노트북 장고 실행 질문입니다.

Viết

·

587

1

수업에 맞게 notebook을 실행해보았는데 계속 에러가 나네요.

 

아무리 찾아봐도 고칠수없어 질문 드립니다.

---------------------------------------------------------------------------
SynchronousOnlyOperation                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\envs\askcompany\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

C:\ProgramData\Anaconda3\envs\askcompany\lib\site-packages\IPython\lib\pretty.py in pretty(self, obj)
    392                         if cls is not object \
    393                                 and callable(cls.__dict__.get('__repr__')):
--> 394                             return _repr_pprint(obj, self, cycle)
    395 
    396             return _default_pprint(obj, self, cycle)

C:\ProgramData\Anaconda3\envs\askcompany\lib\site-packages\IPython\lib\pretty.py in _repr_pprint(obj, p, cycle)
    698     """A pprint that just redirects to the normal repr function."""
    699     # Find newlines and replace them with p.break_()
--> 700     output = repr(obj)
    701     lines = output.splitlines()
    702     with p.group():

C:\ProgramData\Anaconda3\envs\askcompany\lib\site-packages\django\db\models\query.py in __repr__(self)
    250 
    251     def __repr__(self):
--> 252         data = list(self[:REPR_OUTPUT_SIZE + 1])
    253         if len(data) > REPR_OUTPUT_SIZE:
    254             data[-1] = "...(remaining elements truncated)..."

C:\ProgramData\Anaconda3\envs\askcompany\lib\site-packages\django\db\models\query.py in __iter__(self)
    274                - Responsible for turning the rows into model objects.
    275         """
--> 276         self._fetch_all()
    277         return iter(self._result_cache)
    278 

C:\ProgramData\Anaconda3\envs\askcompany\lib\site-packages\django\db\models\query.py in _fetch_all(self)
   1259     def _fetch_all(self):
   1260         if self._result_cache is None:
-> 1261             self._result_cache = list(self._iterable_class(self))
   1262         if self._prefetch_related_lookups and not self._prefetch_done:
   1263             self._prefetch_related_objects()

C:\ProgramData\Anaconda3\envs\askcompany\lib\site-packages\django\db\models\query.py in __iter__(self)
     55         # Execute the query. This will also fill compiler.select, klass_info,
     56         # and annotations.
---> 57         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
     58         select, klass_info, annotation_col_map = (compiler.select, compiler.klass_info,
     59                                                   compiler.annotation_col_map)

C:\ProgramData\Anaconda3\envs\askcompany\lib\site-packages\django\db\models\sql\compiler.py in execute_sql(self, result_type, chunked_fetch, chunk_size)
   1133             cursor = self.connection.chunked_cursor()
   1134         else:
-> 1135             cursor = self.connection.cursor()
   1136         try:
   1137             cursor.execute(sql, params)

C:\ProgramData\Anaconda3\envs\askcompany\lib\site-packages\django\utils\asyncio.py in inner(*args, **kwargs)
     20             else:
     21                 if event_loop.is_running():
---> 22                     raise SynchronousOnlyOperation(message)
     23             # Pass onwards.
     24             return func(*args, **kwargs)

SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
pythondockerreactdjango

Câu trả lời 3

1

rhyou104706님의 프로필 이미지
rhyou104706
Người đặt câu hỏi

해결했습니다.

비동기 버전이 django 3.01부터 적용되었는데 제버전은 3.0 이였네요

django 최신버전으로 다운하니까 문제 해결되었습니다.

pyhub님의 프로필 이미지
pyhub
Người chia sẻ kiến thức

아. 그렇군요. 고생하셨습니다. 👍

1

rhyou104706님의 프로필 이미지
rhyou104706
Người đặt câu hỏi

아무리 해 보아도 되지 않네요

가상환경의 ipython 에서 주피터 노트북 코드를 복사 붙여넣기 해보면 이상없이 작동하는데

이상하게 주피터 노트북에서만 안되네요;;

 

재시작도 해보았고, 재설치 해보았고, os.environ 확인해 보아서 재대로 들어갔는지 확인도 해보았는데도

안되네요..

0

pyhub님의 프로필 이미지
pyhub
Người chia sẻ kiến thức

안녕하세요.

공식문서 https://docs.djangoproject.com/en/4.0/topics/async/ 에서도 안내되듯이, DJANGO_ALLOW_ASYNC_UNSAFE 환경변수를 아래와 같이 추가로 세팅하셨다면 해당 오류는 발생하시지 않습니다.

혹시 DJANGO_ALLOW_ASYNC_UNSAFE 환경변수를 설정하는 코드를 추가하신 후에, 해당 셀부터 django.setup() 셀까지 다시 실행하지 않으신 것은 아니실까요? 쥬피터 노트북의 상단 kernal 메뉴의 "Restart & Run All" 메뉴를 통해 다시 실행해보시겠어요?

 

아래와 같이 DJANGO_ALLOW_ASYNC_UNSAFE 환경변수를 세팅하지 않으면 말씀하신 오류가 발생하고

해당 환경변수를 세팅하면 오류없이 쿼리셋 코드가 동작을 할 것입니다.

차근차근 확인해보세요.

화이팅입니다. :-)

Hình ảnh hồ sơ của rhyou104706
rhyou104706

câu hỏi đã được viết

Đặt câu hỏi