<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">Existuje na to i opravdu kladivo:
      souborovy zamek.<br>
      <br>
      Na lokalnim souborovem systemu mas zaruceno, ze v kriticke sekci
      se bude pohybovat jen jeden thread. Pouzivam i u webovych aplikaci
      v sekcich, kde potrebuju uplnou jistotu.<br>
      <br>
      Implementaci prevzatou z webu najdes napr. v me sade nastroju:<br>
      <br>
         
      <a class="moz-txt-link-freetext" href="https://github.com/tuttle/python-useful/blob/master/useful/filelock.py">https://github.com/tuttle/python-useful/blob/master/useful/filelock.py</a><br>
      <br>
      Lze komplet nainstalovat prikazem:<br>
      <br>
      <tt>$ pip install useful</tt><tt><br>
      </tt><br>
      Drobna ukazecka:<br>
      <br>
      <tt>$ virtualenv /tmp/test-lock && /tmp/test-lock/bin/pip
        install useful && /tmp/test-lock/bin/python</tt><tt><br>
      </tt><tt>New python executable in /tmp/test-lock/bin/python</tt><tt><br>
      </tt><tt>Installing setuptools, pip...done.</tt><tt><br>
      </tt><tt>Downloading/unpacking useful</tt><tt><br>
      </tt><tt>  Downloading useful-0.6.4-py2-none-any.whl (40kB): 40kB
        downloaded</tt><tt><br>
      </tt><tt>Installing collected packages: useful</tt><tt><br>
      </tt><tt>Successfully installed useful</tt><tt><br>
      </tt><tt>Cleaning up...</tt><tt><br>
      </tt><tt>Python 2.7.6 (default, Mar 22 2014, 22:59:38) </tt><tt><br>
      </tt><tt>[GCC 4.8.2] on linux2</tt><tt><br>
      </tt><tt>Type "help", "copyright", "credits" or "license" for more
        information.</tt><tt><br>
      </tt>
      <pre style="color:#000000;background:#ffffff;"><pre><span style="color:#808030; ">></span><span style="color:#808030; ">></span><span style="color:#808030; ">></span> <span style="color:#800000; font-weight:bold; ">import</span> time
<span style="color:#808030; ">></span><span style="color:#808030; ">></span><span style="color:#808030; ">></span> <span style="color:#800000; font-weight:bold; ">from</span> multiprocessing<span style="color:#808030; ">.</span>dummy <span style="color:#800000; font-weight:bold; ">import</span> Pool as ThreadPool
<span style="color:#808030; ">></span><span style="color:#808030; ">></span><span style="color:#808030; ">></span> 
<span style="color:#808030; ">></span><span style="color:#808030; ">></span><span style="color:#808030; ">></span> <span style="color:#800000; font-weight:bold; ">from</span> useful<span style="color:#808030; ">.</span>filelock <span style="color:#800000; font-weight:bold; ">import</span> FileLock
<span style="color:#808030; ">></span><span style="color:#808030; ">></span><span style="color:#808030; ">></span> 
<span style="color:#808030; ">></span><span style="color:#808030; ">></span><span style="color:#808030; ">></span> <span style="color:#800000; font-weight:bold; ">def</span> worker_normal<span style="color:#808030; ">(</span>job<span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>     <span style="color:#800000; font-weight:bold; ">print</span> <span style="color:#0000e6; ">"Start"</span><span style="color:#808030; ">,</span> job
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>     time<span style="color:#808030; ">.</span>sleep<span style="color:#808030; ">(</span><span style="color:#008c00; ">1</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>     <span style="color:#800000; font-weight:bold; ">print</span> <span style="color:#0000e6; ">"End"</span><span style="color:#808030; ">,</span> job
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>     <span style="color:#800000; font-weight:bold; ">return</span> job<span style="color:#808030; ">.</span>lower<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span> 
<span style="color:#808030; ">></span><span style="color:#808030; ">></span><span style="color:#808030; ">></span> <span style="color:#800000; font-weight:bold; ">def</span> worker_critical<span style="color:#808030; ">(</span>job<span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>     with FileLock<span style="color:#808030; ">(</span><span style="color:#0000e6; ">'/tmp/test-lock'</span><span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>         <span style="color:#800000; font-weight:bold; ">print</span> <span style="color:#0000e6; ">"Start"</span><span style="color:#808030; ">,</span> job
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>         time<span style="color:#808030; ">.</span>sleep<span style="color:#808030; ">(</span><span style="color:#008c00; ">1</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>         <span style="color:#800000; font-weight:bold; ">print</span> <span style="color:#0000e6; ">"End"</span><span style="color:#808030; ">,</span> job
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>         <span style="color:#800000; font-weight:bold; ">return</span> job<span style="color:#808030; ">.</span>lower<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span> 
<span style="color:#808030; ">></span><span style="color:#808030; ">></span><span style="color:#808030; ">></span> <span style="color:#800000; font-weight:bold; ">def</span> run_with<span style="color:#808030; ">(</span>worker<span style="color:#808030; ">)</span><span style="color:#808030; ">:</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>     start <span style="color:#808030; ">=</span> time<span style="color:#808030; ">.</span>time<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>     pool <span style="color:#808030; ">=</span> ThreadPool<span style="color:#808030; ">(</span><span style="color:#008c00; ">5</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>     <span style="color:#800000; font-weight:bold; ">print</span> pool<span style="color:#808030; ">.</span><span style="color:#e34adc; ">map</span><span style="color:#808030; ">(</span>worker<span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'ABCDEFGHI'</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>     pool<span style="color:#808030; ">.</span>close<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>     pool<span style="color:#808030; ">.</span>join<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span>     <span style="color:#800000; font-weight:bold; ">print</span> <span style="color:#0000e6; ">"Completion time with %s: %d secs"</span> <span style="color:#808030; ">%</span> <span style="color:#808030; ">(</span>worker<span style="color:#808030; ">,</span> time<span style="color:#808030; ">.</span>time<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span><span style="color:#808030; ">-</span>start<span style="color:#808030; ">)</span>
<span style="color:#808030; ">.</span><span style="color:#808030; ">.</span><span style="color:#808030; ">.</span> 
<span style="color:#808030; ">></span><span style="color:#808030; ">></span><span style="color:#808030; ">></span> run_with<span style="color:#808030; ">(</span>worker_normal<span style="color:#808030; ">)</span>
Start Start AStart B
 Start
 DC

Start E
EndEnd End  CEndEnd A 
D
BE

Start F

Start G
 Start H
Start I
End F
 EndEnd  G
H
End I
<span style="color:#808030; ">[</span><span style="color:#0000e6; ">'a'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'b'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'c'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'d'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'e'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'f'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'g'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'h'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'i'</span><span style="color:#808030; ">]</span>
Completion time with <span style="color:#808030; "><</span>function worker_normal at <span style="color:#008000; ">0xb6f9dae4</span><span style="color:#808030; ">></span><span style="color:#808030; ">:</span> <span style="color:#008c00; ">2</span> secs
<span style="color:#808030; ">></span><span style="color:#808030; ">></span><span style="color:#808030; ">></span> run_with<span style="color:#808030; ">(</span>worker_critical<span style="color:#808030; ">)</span>
Start A
End A
Start F
End F
Start G
End G
Start H
End H
Start I
End I
Start E
End E
Start D
End D
Start C
End C
Start B
End B
<span style="color:#808030; ">[</span><span style="color:#0000e6; ">'a'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'b'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'c'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'d'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'e'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'f'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'g'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'h'</span><span style="color:#808030; ">,</span> <span style="color:#0000e6; ">'i'</span><span style="color:#808030; ">]</span>
Completion time with <span style="color:#808030; "><</span>function worker_critical at <span style="color:#008000; ">0xb6f9ddf4</span><span style="color:#808030; ">></span><span style="color:#808030; ">:</span> <span style="color:#008c00; ">9</span> secs
<span style="color:#808030; ">></span><span style="color:#808030; ">></span><span style="color:#808030; ">></span>
</pre></pre>
      <br>
      V.<br>
      <br>
      On 9.2.2015 13:43, Petr Blahos wrote:<br>
    </div>
    <blockquote
cite="mid:CAE1QKmSz5kjHJXVTmi_aF89K9Fzd=YRXLXWb-0cnfdtzMBKyXQ@mail.gmail.com"
      type="cite">
      <div dir="ltr">Ahoj,
        <div><br>
        </div>
        <div>no, jestli ten kód vypadá opravdu přesně takto, tak dělá
          tohle:</div>
        <div>* vytvoří úplně nový zámek a acquirene ho.</div>
        <div>* udělá to sql</div>
        <div>* releasene zámek.</div>
        <div>No a druhý případný thread dělá to stejné:</div>
        <div>
          <div>* vytvoří úplně nový zámek a acquirene ho.</div>
          <div>* udělá to sql</div>
          <div>* releasene zámek.</div>
        </div>
        <div>Takže nic ničemu v ničem nezabrání. </div>
        <div><br>
        </div>
        <div>Tohle se ale dělá transakcema, a pokud máš MyISAm engine,
          tak spíš zamčením </div>
        <div>patřičných tabulek:</div>
        <div>"LOCK TABLES platby READ"</div>
        <div>...</div>
        <div>"UNLOCK TABLES"</div>
        <div><br>
        </div>
        <div>--<br>
        </div>
        <div>Petr</div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
      </div>
      <div class="gmail_extra"><br>
        <div class="gmail_quote">2015-02-09 11:42 GMT+01:00 mtip <span
            dir="ltr"><<a moz-do-not-send="true"
              href="mailto:mtip@atlas.cz" target="_blank">mtip@atlas.cz</a>></span>:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">Ahoj,<br>
            narazil jsem na problém se zámkem.<br>
            <br>
            Mám  aplikaci  na web serveru, která ukládá příznak booolean
            Zaplaceno<br>
            do MySQL databáze.<br>
            Občas  se ale stane, že potvrzení platby přijde najednou ve
            stejný čas<br>
            ze dvou zdrojů. Chtěl jsem to vyřešit zámkem, což ale
            nepomůže.<br>
            <br>
            Princip kódu:<br>
            <br>
            from threading import Lock<br>
            lock = Lock()<br>
            lock.acquire()<br>
            try:<br>
                mysql_cursor.execute("SELECT Zaplaceno FROM platby WHERE
            Id=%s", (1,))<br>
                if mysql_cursor.fetchall()[0]['Zaplaceno'] == 0:<br>
                    mysql_cursor.execute("UPDATE Zaplaceno=1 FROM platby
            WHERE Id=%s", (1,))<br>
                    mysql_connection.commit()<br>
                    # dalsi zpracovani platby...<br>
            finally:<br>
                lock.release()<br>
            <br>
            <br>
            <br>
            Pokud je request spuštěn 2x ve stený čas provede se
            zpracování platby 2x.<br>
            Přičemž ale SELECT je pod zámkem, takže by se neměl provést
            mezi SELECT a UPDATE druhého threadu.<br>
            Netušíte, kde může být problém?<br>
            <br>
            Díky za odpovědi.<br>
            <br>
            Mirek<br>
            <br>
            _______________________________________________<br>
            Python mailing list<br>
            <a moz-do-not-send="true" href="mailto:python@py.cz">python@py.cz</a><br>
            <a moz-do-not-send="true"
              href="http://www.py.cz/mailman/listinfo/python"
              target="_blank">http://www.py.cz/mailman/listinfo/python</a><br>
            <br>
            Visit: <a moz-do-not-send="true" href="http://www.py.cz"
              target="_blank">http://www.py.cz</a><br>
          </blockquote>
        </div>
        <br>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Python mailing list
<a class="moz-txt-link-abbreviated" href="mailto:python@py.cz">python@py.cz</a>
<a class="moz-txt-link-freetext" href="http://www.py.cz/mailman/listinfo/python">http://www.py.cz/mailman/listinfo/python</a>

Visit: <a class="moz-txt-link-freetext" href="http://www.py.cz">http://www.py.cz</a></pre>
    </blockquote>
    <br>
    <br>
    <pre class="moz-signature" cols="75">-- 
:  Vladimir Macek  :  <a class="moz-txt-link-freetext" href="http://macek.sandbox.cz">http://macek.sandbox.cz</a>  :  +420 608 978 164
:  UNIX && Dev || Training  :  Python, Django  :  PGP key 97330EBD
</pre>
  </body>
</html>