AboutPython

· AboutPython
하나의 스레드가 두 개의 작업을 연속적으로 수행한 것에 비해, 두 개 스레드가 각각 하나의 작업을 수행했을 때 더 오래걸렸다고 합니다. 왜 이런 일이 발생했을까요? import random import threading import time def working(): max([random.random() for i in range(500000000)]) # 1 Thread s_time = time.time() working() working() e_time = time.time() print(f'{e_time - s_time:.5f}') # 2 Threads s_time = time.time() threads = [] for i in range(2): threads.append(threading.Thre..
· AboutPython
Python에서 수명이 다한 객체는 어떻게 메모리가 회수되나요? 더보기 Python에서 수명이 다한 객체는 Reference Counting을 통해 메모리가 회수됩니다. 모든 객체마다 자신이 참조되고 있는 개수를 들고 있다가 이 숫자가 0이 되면 메모리에서 삭제하는 방식 입니다. Cyclic Garbage Collection 에 대해 설명해보세요 더보기 linked list 객체와 같이 순환 참조(reference cycle)이 있는 객체에 대해서는 Reference Counting 만으로 객체를 제거할 수 없기 때문에 cyclic garbage collection을 사용합니다. 모든 객체에 대해 reference를 graph를 그리며, 접근 불가능한 cycle을 찾아 garbage에 해당하는 객체를 찾..
· AboutPython
Union을 쓰는 이유 # multi_param.py from typing import Union from fastapi import FastAPI app = FastAPI() @app.get("/users/{user_id}/items/{item_id}") def read_user_item(user_id: int, item_id: str, q: Union[str, None] = None, short: bool = False): item = {"item": item_id, "owner_id": user_id} if q: item.update({"q": q}) if not short: item.update( {"description": "This is an amazing item that has a long..
· AboutPython
[find] find( 찾을 문자, 찾기 시작하는 위치 ) >>> s = 'abcde fghijklmnj' >>> s.find('j') 10 >>> s.find('j',10) 10 >>> s.find('j',11) 15 >>> s.find('z') -1 [startswith] startswith( "문자" 로 시작하는지, 시작지점 ) >>> s = 'abcde fghijklmnj' >>> s.startswith('a') True >>> s.startswith('a',1) False >>> s.startswith('c',2) True [endswith] endwith('문자'로 끝나는지, 탐색을 시작하는 인덱스, 탐색이 끝나는 인덱스) >>> s = 'abcde fghijklmnj' >>> s.endswi..
scone
'AboutPython' 카테고리의 글 목록 (2 Page)