
    rh(                        S SK Jr  / SQrS SKrS SKrSSSSS.S jrSSSSS.S jrS	 rS
 rS r	 " S S\R                  5      r\S:X  a  S SKr\R                  " \5        gg)    )annotations)runParallelrunNonParallelcpusN   FupdateFunctionupdateMultiplyunpackIterableupdateSendsIterablec          	     4  ^ ^^^^^^^ [        5       mTS:X  d$  [        R                  " 5       R                  (       a  [	        T UTTUTS9$ [        T 5      mSnTc  Tm/ mUU UUUUU4S jnU" S5        SSKJnJn	  U" TS9 n
U	" U5      mUT:  an  [        UTT-  -   T5      n[        Xk5      nU(       a  U
" UU 4S jU 5       5      nOU
" UU 4S	 jU 5       5      nUnTR                  U5        U" U5        UT:  a  Mn  SSS5        T$ ! , (       d  f       T$ = f)
aE  
runs parallelFunction over iterable in parallel, optionally calling updateFunction after
each common.cpus * updateMultiply calls.

Setting updateMultiply too small can make it so that cores wait around when they
could be working if one CPU has a particularly hard task.  Setting it too high
can make it seem like the job has hung.

updateFunction should take three arguments: the current position, the total to run,
and the most recent results.  It does not need to be pickleable, and in fact,
a bound method might be very useful here.  Or updateFunction can be "True"
which just prints a generic message.

If unpackIterable is True then each element in iterable is considered a list or
tuple of different arguments to parallelFunction.

If updateSendsIterable is True then the update function will get the iterable
content, after the output.

As of Python 3, partial functions are pickleable, so if you need to pass the same
arguments to parallelFunction each time, make it a partial function before passing
it to runParallel.

Note that parallelFunction, iterable's contents, and the results of calling parallelFunction
must all be pickleable, and that if pickling the contents or
unpickling the results takes a lot of time, you won't get nearly the speedup
from this function as you might expect.  The big culprit here is definitely
music21 streams.

>>> files = ['bach/bwv66.6', 'schoenberg/opus19', 'AcaciaReel']
>>> def countNotes(fn):
...     c = corpus.parse(fn)  # this is the slow call that is good to parallelize
...     return len(c.recurse().notes)
>>> #_DOCS_SHOW outputs = common.runParallel(files, countNotes)
>>> outputs = common.runNonParallel(files, countNotes) #_DOCS_HIDE cannot pickle doctest funcs.
>>> outputs
[165, 50, 131]

Set updateFunction=True to get an update every 3 * numCpus (-1 if > 2)

>>> #_DOCS_SHOW outputs = common.runParallel(files, countNotes, updateFunction=True)
>>> outputs = common.runNonParallel(files, countNotes, updateFunction=True) #_DOCS_HIDE
Done 0 tasks of 3
Done 3 tasks of 3

With a custom updateFunction that gets each output:

>>> def yak(position, length, output):
...     print("%s:%s %s is a lot of notes!" % (position, length, output))
>>> #_DOCS_SHOW outputs = common.runParallel(files, countNotes, updateFunction=yak)
>>> outputs = common.runNonParallel(files, countNotes, updateFunction=yak) #_DOCS_HIDE
0:3 165 is a lot of notes!
1:3 50 is a lot of notes!
2:3 131 is a lot of notes!

Or with updateSendsIterable, we can get the original files data as well:

>>> def yik(position, length, output, fn):
...     print("%s:%s (%s) %s is a lot of notes!" % (position, length, fn, output))
>>> #_DOCS_SHOW outputs = common.runParallel(files, countNotes, updateFunction=yik,
>>> outputs = common.runNonParallel(files, countNotes, updateFunction=yik, #_DOCS_HIDE
...             updateSendsIterable=True)
0:3 (bach/bwv66.6) 165 is a lot of notes!
1:3 (schoenberg/opus19) 50 is a lot of notes!
2:3 (AcaciaReel) 131 is a lot of notes!

unpackIterable is useful for when you need to send multiple values to your function
call as separate arguments.  For instance, something like:

>>> def pitchesAbove(fn, minPitch):  # a two-argument function
...     c = corpus.parse(fn)  # again, the slow call goes in the function
...     return len([p for p in c.pitches if p.ps > minPitch])

>>> inputs = [('bach/bwv66.6', 60),
...           ('schoenberg/opus19', 72),
...           ('AcaciaReel', 66)]
>>> #_DOCS_SHOW outputs = common.runParallel(inputs, pitchesAbove, unpackIterable=True)
>>> outputs = common.runNonParallel(inputs, pitchesAbove, unpackIterable=True) #_DOCS_HIDE
>>> outputs
[99, 11, 123]
   r   r   Nc                
  > TSL a  [        U T/5      n[        SU ST 35        g TS;  aX  [        U T	T-  -
  U 5       HA  nUS:  a  M  U[        T5      :  a  S nOTU   nT
SL a  T" UTU5        M3  T" UTUTU   5        MC     g g )NTDone 
 tasks of FNr   Fminprintrangelen)ii	tasksDonethisPosition
thisResult
iterLengthiterablenumCpusresultsListr	   r
   r   s       Q/home/james-whalen/.local/lib/python3.13/site-packages/music21/common/parallel.py
callUpdaterunParallel.<locals>.callUpdate}   s    T!R,-IE)Jzl;<=0 %bNW,D&Er J!#3{#33!%J!,\!:J&%/"<ZH"<ZR^I_` !K 1    )Paralleldelayed)n_jobsc              3  4   >#    U  H  nT" TU   6 v   M     g 7fN .0idelayFunctionr   s     r    	<genexpr>runParallel.<locals>.<genexpr>   s     Hx!-!5xs   c              3  :   >#    U  H  nT" TU   5      v   M     g 7fr(   r)   r*   s     r    r.   r/      s     Gh-44hs   )r   multiprocessingcurrent_processdaemonr   r   joblibr$   r%   r   r   extend)r   parallelFunctionr	   r
   r   r   totalRunr!   r$   r%   paraendPositionrangeGen_rr-   r   r   r   s   ` `` `        @@@@r    r   r      s"   h fG!|668??h(8-;-;-;2E	G 	G XJH# Ka a& qM(		!T 01#h>)AA:NKX3HHxHHGhGG"Hr"x  # 
"  
"	! s   A;D
Dc                  ^ ^^^^	^
 [        T 5      m	/ m
U	U U
UUU4S jnU" S5        [        T	5       H:  nU(       a	  U" T U   6 nOU" T U   5      nT
R                  U5        U" US-   5        M<     T
$ )z
This is intended to be a perfect drop in replacement for runParallel, except that
it runs on one core only, and not in parallel.

Used automatically if we're already in a parallelized function.
c                  > U T-  S:w  a  g TSL a  [        U T/5      n[        SU ST 35        g TS;  aU  [        U T-
  U 5       HA  nUS:  a  M  U[        T5      :  a  S nOTU   nT	SL a  T" UTU5        M3  T" UTUTU   5        MC     g g )Nr   Tr   r   r   Fr   )
r   r   r   r   r   r   r   r	   r
   r   s
       r    r!   "runNonParallel.<locals>.callUpdate   s    !#T!R,-IE)Jzl;<=0 %b>&92 >!#3{#33!%J!,\!:J&%/"<ZH"<ZR^I_` !? 1r#   r   r   )r   r   append)r   r6   r	   r
   r   r   r!   r,   r;   r   r   s   ` `` `   @@r    r   r      sx     XJKa a, qM:!8A;/B!(1+.B21q5  r#   c                 F    [         R                  " 5       n U S:  a  U S-
  $ U $ )z^
Returns the number of CPUs or if >= 3, one less (to leave something out for multiprocessing)
r   r   )r1   	cpu_count)cpuCounts    r    r   r      s(     ((*H1}!|r#   c                v    SSK Jn  UR                  U 5      n[        UR	                  5       R
                  5      $ )Nr   )corpus)music21rD   parser   recursenotes)fnrD   cs      r    _countNrK      s*    RAqyy{  !!r#   c                     U S:  a  gUS;  a  gg)Nr   Fzbach/bwv66.6zschoenberg/opus19
AcaciaReelTr)   )r,   rI   s     r    _countUnpackedrO      s    Av	DDr#   c                  &    \ rS rSrS rS rS rSrg)Test   c                @   / SQnSSK JnJn  [        X5      nU R	                  U/ SQ5        [        XU R
                  S9  [        XU R                  SS9  [        [        [        U5      5      USS9nU R	                  [        U5      S	5        U R                  S
U5        g )NrM   r   )rK   rO      2      )r	   T)r	   r   )r   r   F)music21.common.parallelrK   rO   r   assertEqual_customUpdate1_customUpdate2list	enumerater   assertNotIn)selffilesrK   rO   outputpasseds         r    &x_figure_out_segfault_testMultiprocess+Test.x_figure_out_segfault_testMultiprocess   s    CCU,0E#'#6#6	8E#'#6#6(,	. T)E"23^,02Va('r#   c                t    U R                  US5        U R                  US5        U R                  U/ SQ5        g )Nr   rT   )rY   
assertLessassertIn)r_   r,   totalra   s       r    rZ   Test._customUpdate1  s.    "1fn-r#   c                ,    U R                  U/ SQ5        g )NrM   )rg   )r_   r,   unused_totalunused_outputrI   s        r    r[   Test._customUpdate2  s    bMNr#   r)   N)__name__
__module____qualname____firstlineno__rc   rZ   r[   __static_attributes__r)   r#   r    rQ   rQ      s    (".
Or#   rQ   __main__)
__future__r   __all__r1   unittestr   r   r   rK   rO   TestCaserQ   rn   rE   mainTestr)   r#   r    <module>ry      s    #    $A$%K^ #'q"'U-`*"O8 O8 zT r#   