JavaScript에서의 AsyncIterator(비동기 반복자)

비동기 반복자는 여러개의 데이터를 순차적으로 반복해서 얻을때 비동기적으로 처리하는 반복자를 말합니다. 즉, 비동기적으로 데이터를 하나씩 제공하는 객체가 비동기 반복자입니다. ok?

비동기 반복자가 있기 수만년전에 그냥 반복자가 있었습니다. 그 예시는 다음과 같습니다.

function *getIterator() {
    let i = 0;

    for(let i=0; i<3; i++) {
        yield i;
    }
}

const it = getIterator();

console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next());

for(const it of getIterator()) {
    console.log(it);
}

결과는 다음과 같구요.

코드가 동기적으로 한줄씩 순서대로 실행되는 것을 확인할 수 있습니다.

이제 이번에는 위와 동일한 기능이지만 비동기적으로 실행되는 코드, 즉 비동기 반복자의 예시는 다음과 같습니다.

async function* getIterator() {
    for (let i = 0; i < 3; i++) {
        yield i;
    }
}

const it = getIterator();

async function test1() {
    console.log(await it.next());
    console.log(await it.next());
    console.log(await it.next());
    console.log(await it.next());
}

test1();

async function test2() {
    for await (const it of getIterator()) {
        console.log(it);
    }
}

test2();

일반적인 반복자(동기 반복자)를 정의한 코드에서 단지 async만 붙였습니다. 이렇게 정의한 비동기 반복자를 실행하기 위해서는 역시 비동기 함수가 필요하므로 test1, test2라는 비동기 함수를 이용하고 있습니다. 실행 결과는 다음과 같습니다.

실행 결과를 보면 비동기적으로 실행되는 것을 확인할 수 있습니다.

실무에서는 비동기 반복자를 정의하는 함수의 구현에 비동기적으로 결과를 얻는 fetch 라든지 웹소켓 통신 등의 코드가 들어갑니다. 이를 흉내내기 보면..

async function* getIterator() {
    for (let i = 0; i < 3; i++) {
        const delay = Math.random() * 1000;
        const result = await new Promise(resolve => {
            setTimeout(() => {
                resolve(i);
            }, delay);
        });

        yield result;
    }
}

const it = getIterator();

async function test1() {
    console.log(await it.next());
    console.log(await it.next());
    console.log(await it.next());
    console.log(await it.next());
}

test1();

async function test2() {
    for await (const it of getIterator()) {
        console.log(it);
    }
}

test2();

상기 코드의 결과는 다음과 같습니다.

postgresql, 인덱스 정의 조회

pgAdmin과 같은 GUI 툴에서 표시되지 않는 인덱스를 포함하여 전체 인덱스를 확인하는 쿼리는 다음과 같습니다.

SELECT 
	indexname, indexdef
FROM 
	pg_indexes
WHERE 
	schemaname = '스키마 이름'
	AND tablename = '테이블 이름'

psql에서 스키마 관련 명령

스키마를 생성하는 명령은 다음과 같습니다.

CREATE SCHEMA my_schema;

현재 기본으로 지정된 스키마를 확인하는 명령은 다음과 같습니다.

SHOW search_path;

원하는 스키마를 기본으로 지정하는 명령은 다음과 같습니다.

SET search_path my_schema;

CLI 방식으로 SHP 파일을 PostgreSQL에 import 하기

d:/_t/LSMD_CONT_LDREG_11_202607.shp 경로의 SHP 파일을 temp2라는 데이터베이스에 cadastral이라는 테이블 이름으로 import 하는 명령은 다음과 같습니다.

shp2pgsql -I -s 0 -W "CP949" -g the_geom "d:/_t/LSMD_CONT_LDREG_11_202607.shp" public.cadastral | psql -h localhost -p 5432 -U postgres -d temp2

이렇게 import된 테이블을 GeoService-Xr에서 지오데이터로써 사용하기 위해 다음 쿼리 실행이 필요합니다.

ALTER TABLE public.cadastral RENAME COLUMN gid TO fid;
CREATE INDEX cadastral_Index ON public.cadastral (fid);

three.js, LightProbeGrid API 핵심 코드

이 글은 three.js를 이용한 효과적인 광원 효과를 위해 LightProbeGrid를 WebGPU 렌더러에서 사용할 때 빠른 적용을 위해 작성된 글입니다.

먼저 필요한 API에 대한 import 문입니다.

import { LightProbeGrid } from 'three/examples/jsm/lighting/LightProbeGrid.js';
import { LightProbeGridHelper } from 'three/examples/jsm/helpers/LightProbeGridHelper.js';

관련된 필드는 다음과 같습니다.

private probes?: LightProbeGrid;
private probesHelper?: LightProbeGridHelper;

LightProbeGrid는 정적 장면에 대한 광원 효과입니다. 즉, 장면을 구워야 합니다. 아래의 코드는 장면에 대한 광원을 굽는 코드입니다.

async bake(resolution: number) {
  if (this.probes) {
    this.scene.remove(this.probes);
    this.probes.dispose();
  }

  const probes = new LightProbeGrid(
    5.6, 4.7, 5.6,
    resolution, resolution, resolution
  );

  probes.position.set(0, 2.45, 0);

  await probes.bake(
    this.renderer, this.scene,
    { 
      cubemapSize: 32,
      sampleCount: 512, 
      bounces: 0,
      near: 0.05, 
      far: 20
    }
  );

  probes.visible = params.enabled;
  this.scene.add(probes);

  this.probes = probes;

  // Update debug visualization

  if (!this.probesHelper) {
    this.probesHelper = new LightProbeGridHelper(probes);
    this.probesHelper.visible = params.showProbes;
    this.scene.add(this.probesHelper);
  } else {
    this.probesHelper.probes = probes;
    this.probesHelper.update();
  }
}

위의 코드에서 사용된 설정값에 대한 객체는 다음과 같습니다.

const params = {
  enabled: true,
  showProbes: true,
  resolution: 6
};

GUI를 통해 설정값을 조정하기 위한 코드는 다음과 같습니다.

private setupGUI() {
  const gui = this.renderer.inspector.createParameters('Light Probes');
  gui.add(params, 'enabled').name('GI').onChange((value: boolean) => {
    if(this.probes) this.probes.visible = value;
  });

  gui.add(params, 'resolution', 2, 12, 1).name('Resolution').onChange((value: number) => {
    this.bake(value);
  });

  gui.add(params, 'showProbes').name('Show Probes').onChange((value: boolean) => {
    if(this.probesHelper) this.probesHelper.visible = value;
  });
}