ÆÄÀ̽ã Áú¹®

   Á¶È¸ 3440   Ãßõ 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 977/5686
2015-12   1513413   ¹é¸Þ°¡
2014-05   4977297   Á¤ÀºÁØ1
2019-10   3274   maronet
2022-07   1669   ½º¹«ÇÁ
2013-12   7550   ¿¹»ó¾Æºü
2018-08   2945   À¾³»³ë´Â¿Àºü
03-19   1159   lenux8
2011-06   9363   ¸¶ÀÌÄÚÄÚ
2013-12   4338   ¾öû³­x
2016-06   10129   audacity
2021-01   1998   µà¾ó¸Ç
2013-12   3841   ÀÌ»óºÀ
2017-05   3575   ±Ç¿ë1
2019-10   2972   °­Çѱ¸
03-29   1274   metaljw
2011-07   5731   Àΰ£±è»ó¼ö
2013-12   9205   ÃÖâÇö
2019-10   6143   ½ÅÀº¿Ö
2013-12   14274   À©µµ¿ì10
2016-06   4282   wdm42
2017-05   2862   ´ÊÀáÀڴ¾ÆÀÌ
2021-01   2355   º°µéÀǼӻèÀÓ