ÆÄÀ̽ã Áú¹®

   Á¶È¸ 3435   Ãßõ 0    

안녕하세요

파이썬으로 제가 사용하는 계산을 자동으로 처리하려고 하는데,

지금까지는 제가 이렇게 사용했습니다. (제대로 짠건지는 모르겠지만 일단 작동은 잘되네요).

계산 한번에 10분이 걸린다고 하면 (코어 1개만 사용합니다), 순차적으로 10분씩 걸려서 끝날 때 까지 너무 오래걸립니다.

cpu 코어가 32개라서 동시에 계산을 하고싶은데, 가능한가요??

조언부탁드립니다.

감사합니다.



ªÀº±Û Àϼö·Ï ½ÅÁßÇÏ°Ô.
chis 2020-06
½Ã°£ÀÌ Á» µÈÁö¶ó Àúµµ ±â¾ïÀÌ °¡¹°°¡¹°Çѵ¥¿ä.
ffmpegÀ¸·Î ´Ù¼öÀÇ À½¼ºÆÄÀÏÀ» Æ®·£½ºÄÚµù ÇÏ·Á°í Çߴµ¥ ÄÚ¾î1°³·Î¸¸ ÀÛ¾÷À» ÇÏ´Ï±î ¿À·¡°É·Á¼­
¸ÖƼÄÚ¾î·Î Çغ¼¶ó°í »ðÁúÀ» Á» Çß¾ú´Âµ¥ °á·ÐÀº ½ÇÆÐÇß½À´Ï´Ù..
¿ÜºÎ ÇÁ·Î±×·¥À» ºÒ·¯¿À´Â°Ç ÀÌ»óÇÏ°Ô ¾ÈµÇ´õ¶ó°í¿ä..
¹°·Ð Á¦°¡ ½Ç·ÂÀÌ ¸¹ÀÌ ¹ÌõÇÑ°í·Î °¡´ÉÇÑ°ÍÀ» ¸øÇÑ°ÍÀÏ °¡´É¼ºÀÌ Å©±äÇÏÁö¸¸ Âü°íÇϼ¼¿ä..
Æз¯·¼ ÇÁ·Î±×·¡¹ÖÀ» ÇϼžßÁÒ.
ÀϹÝÀûÀÎ ÇÁ·Î±×·¥µéÀº CPU ÇÑ°³¸¸ ¾¹´Ï´Ù.

https://blog.naver.com/PostView.nhn?blogId=woosoung1993&logNo=221639525899
¹«¾Æ 2020-06
ÇϳªÀÇ ½ºÅ©¸³Æ®·Î for ¹® 10°³ÀÇ ÀÛ¾÷ ÇÒ °ÍÀ»
for ¹® ¾øÀÌ Çϳª¸¸ ÇÒ ¼ö ÀÖ´Â ½ºÅ©¸³Æ®¸¦ 10°³ µû·Î (µ¿½Ã¿¡ ¹é±×¶ó¿îµå·Î) µ¹¸®´Â °ÍÀÌ ÁÁÁö¾ÊÀ»±î¿ä??
Wheein 2020-06
½ÇÁ¦ ÀÏÀº ´Ù¸¥ ÇÁ·Î¼¼½º°¡ ÇÏ°í, ÆÄÀ̽ãÀº stdin, stdout ¸¸ °ü¸®ÇÏ´Â °ÍÀÌ´Ï thread ¸¸ ³ª´©¾îµµ µÉ °Í °°½À´Ï´Ù.

from concurrent.futures import ThreadPoolExecutor

def _do_work(i):
    command = "..."
    p = Popen(command, ...)
    p.communicate(...)

with ThreadPoolExecutor(32) as pool:
    pool.map(_do_work, [110, 120, ...])

´ë°­ ¿ä·± ´À³¦À¸·Î¿ä
Àß ¸¸µå½Å °Í °°Àºµ¥¿ä. ÇÁ·Î¼¼½º´Â 10°³°¡ »ý¼ºµÇ¾î¼­ µ¹¾Æ°¥ÅÙµ¥... ¾Æ¸¶ I/O¶§¹®¿¡ blockµÇ¾î¼­ ±× °á°ú¸¦ pythonÀÌ °¡Á®°¡ ÁÙ ¶§±îÁö ±â´Ù¸®±â ¶§¹®¿¡ ¼øÂ÷ÀûÀ¸·Î µ¹¾Æ°¡´Â °Íó·³ º¸ÀÌ´Â °Í °°½À´Ï´Ù. I/O¸¦ ÆÄÀÏ¿¡¼­ ¹Þ°í ÆÄÀÏ·Î ÀúÀåµÇµµ·Ï ÇÑ´Ù¸é, °¢°¢ÀÇ ÇÁ·Î¼¼½º´Â µû·Î µ¹¾Æ°¥ °Ì´Ï´Ù. ´Ù µ¹°í ÆÄÀÏ¿¡¼­ °á°ú¸¦ È®ÀÎÇÏ¸é µÇ°ÚÁÒ.
ÇÏÁö¸¸, processµéÀÌ °°Àº ¸®¼Ò½º¸¦ °øÀ¯Çؼ­ »ç¿ëÇØ¾ß ÇÑ´Ù¸é, °á°ú´Â µ¿ÀÏÇÒ °Å¶ó°í »ý°¢µË´Ï´Ù.

Àý´ë ±×·² ÀÏÀÌ ¾È »ý±â°Ô ¿¹¸¦ µé¾î ¾Æ·¡Ã³·³ ¹«ÇÑ loopÀ» Çϳª ¸¸µé°í...

root@ubuntu-20:~/python_subprocess# cat loop.c
#include <unistd.h>
#include <stdio.h>

int main(void)
{
  while (1) {
    usleep(1);
  }
}

ÄÄÆÄÀÏ ÇÑ ´ÙÀ½, ÆÄÀ̽㿡¼­ µ¹·Áº¸¸é...
root@ubuntu-20:~/python_subprocess# python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> proc_list = []
>>> for x in range(10):
...    proc_list.append(subprocess.Popen(["./loop"]))
...
>>> proc_list
[<subprocess.Popen object at 0x7f2bdbc332b0>, <subprocess.Popen object at 0x7f2bdbc67520>, <subprocess.Popen object at 0x7f2bdbc67cd0>, <subprocess.Popen object at 0x7f2bdbbe4070>, <subprocess.Popen object at 0x7f2bdbbe40a0>, <subprocess.Popen object at 0x7f2bdbbe4130>, <subprocess.Popen object at 0x7f2bdbbe4190>, <subprocess.Popen object at 0x7f2bdbbe41f0>, <subprocess.Popen object at 0x7f2bdbbe4880>, <subprocess.Popen object at 0x7f2bdbbe48b0>]

Àß »ì¾Æ ÀÖ´ÂÁö È®ÀÎ... NoneÀ» ¸®ÅÏÇϸé Á×Áö ¾Ê°í »ì¾Ò´Ù´Â ÀǹÌÀÓ.
>>> [x.poll() for x in proc_list]
[None, None, None, None, None, None, None, None, None, None]
>>>

´Ù¸¥ â¿¡¼­ µ¹¾Æ°¡°í ÀÖ´Â Áö È®ÀÎ...
root@ubuntu-20:~/python_subprocess# ps ax | grep ./loop
  6519 pts/2    S      0:15 ./loop
  6520 pts/2    S      0:15 ./loop
  6521 pts/2    S      0:15 ./loop
  6522 pts/2    S      0:15 ./loop
  6523 pts/2    S      0:15 ./loop
  6524 pts/2    R      0:15 ./loop
  6525 pts/2    S      0:15 ./loop
  6526 pts/2    S      0:15 ./loop
  6527 pts/2    S      0:15 ./loop
  6528 pts/2    S      0:15 ./loop

topÀ¸·Î È®ÀÎÇصµ ÀúÁ¤µµ¸é »óÀ§ 10°³¸¦ Â÷ÁöÇÏ°í ÀÖÀ» °Å°í¿ä.
³×ÀÌÃÄ 2020-06
´äº¯µé °¨»çÇÕ´Ï´Ù. Âü°íÇؼ­ °øºÎÇغ¸°Ú½À´Ù~~


QnA
Á¦¸ñPage 976/5686
2015-12   1505744   ¹é¸Þ°¡
2014-05   4969414   Á¤ÀºÁØ1
2004-03   9729   ÀÌ´öÁø
2007-05   4567   À±È£¿ë
2005-01   5827   ÇÑ»óõ
2007-11   5088   Á¤¿µÃ¶
2008-01   4949   À̽¿±
2008-02   5298   ÃÖÀç±Õ
2006-03   5674   °­½ÂÈÖ
2006-06   5414   ÀÌÁØÈ£
2007-06   7036   ÃÖâÇö
2008-01   5460   ½É±Ô»ó
2008-03   7612   ±è¿ë¼ö_
2002-07   14450   À±¼ºÃ¶
2002-12   12948   ÀÌÁ¾¹Î
2004-02   8485   À̰浿
2004-04   8285   À̺´¼±
2007-05   5335   ÀÌÁ¾¿ø
2005-07   5751   ¿ø³«¿¬
2006-03   5589   À̰浿
2008-01   5673   ¿ÀÁØÈ£
2005-08   5704   ±è°æÈ£