
SwooleHttpClient::download 함수는 동시처리(Priority)를 지원하지 않습니다. 따라서, 동시처리(Priority)를 설정하려면, download 함수를 호출하기 전에 스레드 또는 프로세스를 생성하여 다운로드를 진행해야 합니다.
다음은 스레드를 사용한 예시입니다.
#hostingforum.kr
php
$priority = swoole_coroutine::getPriority();
swoole_coroutine::setPriority(1); // 다운로드를 진행할 때 우선순위를 1로 설정
// 다운로드를 진행하는 스레드
swoole_coroutine::run(function () use ($url) {
$client = new SwooleHttpClient($url);
$client->set(['timeout' => 10]);
$client->get('/');
$file = $client->body;
file_put_contents('downloaded_file.txt', $file);
});
// 다운로드가 완료된 후 우선순위를 원래 설정으로 되돌려줍니다.
swoole_coroutine::setPriority($priority);
다음은 프로세스를 사용한 예시입니다.
#hostingforum.kr
php
$priority = swoole_coroutine::getPriority();
swoole_coroutine::setPriority(1); // 다운로드를 진행할 때 우선순위를 1로 설정
// 다운로드를 진행하는 프로세스
$process = new SwooleProcess(function ($worker) use ($url) {
$client = new SwooleHttpClient($url);
$client->set(['timeout' => 10]);
$client->get('/');
$file = $client->body;
file_put_contents('downloaded_file.txt', $file);
}, false, true);
$process->start();
// 다운로드가 완료된 후 우선순위를 원래 설정으로 되돌려줍니다.
swoole_coroutine::setPriority($priority);
주의: 스레드 또는 프로세스를 생성하고 종료하는 것은 복잡한 작업이므로, 다운로드를 진행하는 스레드 또는 프로세스를 관리하는 코드를 추가로 작성해야 합니다.
2025-04-13 12:56