Best Python code snippet using lemoncheesecake
thread_2.py
Source:thread_2.py
...10##iki threadli bir akıŠörneÄi:Main thread içerisinde 2. bir thread yaratıyorum 11#ekran çıktısındaki tuhaflık threadlerin asenkron biçimde çalıÅması kaynaklıdır 12import threading13import time14def thread_func():15 for i in range(10):16 print(f'new thread: {i}')17 time.sleep(1) #thread_func threadini bir saniye durdurur diÄer threadim çalıÅmaya devam eder18def main():19 t = threading.Thread(target=thread_func) #thread yaratma Åeklim20 t.start() # t nesnemi start diyip çalıÅtırıyorum21 22 for i in range(10):23 print(f'main thread: {i}')24 time.sleep(1) #main threadimi bir saniye durdurur diÄer threadim çalıÅmaya devam eder25main() 26#------------------------------------------------------------- 27#Bir threade parametre geçirmek istersem;args içine tuple Åeklinde yazımı verdim ve thread_fuc methoduna geçti28import threading29import time30def thread_func(msg):31 for i in range(10):32 print(f'{msg}: {i}')33 time.sleep(1)34def main():35 t = threading.Thread(target=thread_func, args=('new thread',)) #tuple aliyor arguman olarak36 t.start()37 38 for i in range(10):39 print(f'main thread: {i}')40 time.sleep(1)41main() 42##bir threade aÅaÄıdaki gibi iki tane parametre geçirmek istersem :43import threading44import time45def thread_func(msg, xxx):46 for i in range(10):47 print(f'{msg}: {i}')48 time.sleep(1)49 print('xxx', '=', xxx)50def main():51 t = threading.Thread(target=thread_func, args=('new thread',), kwargs={'xxx': 100})52 t.start()53 54 for i in range(10):55 print(f'main thread: {i}')56 time.sleep(1)57main()58#aynı fonksiyonu birden fazla thread iÅletebilir. 59#yerel deÄiÅkenler stackte yaratılır ve her threadin stacki farklıdır 60import threading61import time62def foo(msg):63 i = 064 while i < 10:65 print(msg, '=>', i) #her thread yereldegiskenin farkli kopyasini olusturuyor66 i += 167 time.sleep(1)68 69def thread_func():70 foo('N')#her thread foo fonksiyonunu çaÄırıyor71def main():72 t = threading.Thread(target=thread_func)73 t.start()74 foo('M')#her thread foo fonksiyonunu çaÄırıyor75 76main()77#threadlerde global deÄiÅken kullanma kuralı78##global degiskenler ortak 79import threading80import time81i = 0 #her thread diÄerinin arttırdıÄı i deÄiÅkenini alıp arttıryor yerel bir kopya yapmıyor82def foo(msg):83 global i84 85 while i < 10:86 print(msg, '=>', i)87 i += 188 time.sleep(1)89 90def thread_func():91 foo('N')92def main():93 t = threading.Thread(target=thread_func)94 t.start()95 foo('M')96 97main()98##Bir thread ne zaman biter? =>>pythonda tipik olarak bir thread bittiÄinde sonlanır bir müdahale vs olmaz 99#aÅaÄıdaki örnekte bir thread çalıÅırken diÄeri bekliyor.100import threading101import time102 103def thread_func():104 for i in range(10):105 print(i, end=' ')106 time.sleep(1)107def main():108 t = threading.Thread(target=thread_func)109 t.start()110 s = input('Adı Soyadı:')111 print(s) 112main()113#bir thread icindeki herhangi bir exception tüm programı çökertmez yalnızca o threadi sonlandırır. 114import threading115import time116 117def thread_func():118 for i in range(10):119 print(f'new thread: {i}')120 time.sleep(1)121 if i == 5:122 raise ValueError('Exception occurred!') #yalnızca bu thread sonlandırılır123#eÄer main threadde exception olsaydı tüm programım patlardı124def main():125 t = threading.Thread(target=thread_func)126 t.start()127 128 time.sleep(0.5)129 130 for i in range(10):131 print(f'Main thread: {i}')132 time.sleep(1)133 134main()135#Bazı programlama dillerinde ana thread biterse program sonlanır.(C gibi) 136#Bazı programlama dillerinde ana threadin bitmesiyle program sonlanmz ve son thread bitene kadar devam eder.(java,python) 137import threading138import time139 140def thread_func():141 for i in range(10):142 print(f'new thread: {i}')143 time.sleep(1)144def main():145 t = threading.Thread(target=thread_func)146 t.start()147 print('main thread ends...') #ana thread bitti ama diÄer threadimim çalıÅmaya devam etti148 149main()150#daemeon thread :ana thread daemeon thread deÄildir. Processin daemeon olmayan threadi sonlandıÄında iÅ biter 151#is_alive:O anda o thread yaÅıyor mu onu kontrol etmek için kullanılır.True false Åeklinde toggle yapar152import threading153import time154 155def thread_func():156 for i in range(10):157 print(i, end=' ')158 time.sleep(1)159def main():160 t = threading.Thread(target=thread_func)161 print(t.is_alive())162 t.start()163 print(t.is_alive())164 input('Press ENTER to continue...')165 print(t.is_alive())166 167main()168####169import threading170import time171 172def thread_func():173 for i in range(10):174 print(i, end=' ')175 time.sleep(1)176def main():177 t = threading.Thread(target=thread_func)178 t.start() 179 t.join() #zaman verilmezse diÄer threadler bitene kadar main thread bekler. hepsi bitince ok der geçer t.join(x) x saniye bekliyor 180 #â¦t.join(4) 4 saniyeye kadar diÄeri bitmezse main thread kladıÄı yerden devam eder.181 print('Ok')182 ...
test_threading_progress.py
Source:test_threading_progress.py
...16 connect={'yielded': test_yield},17 progress={'total': 2},18 start_thread=False,19 )20 worker = thread_func()21 with qtbot.waitSignal(worker.yielded):22 worker.start()23 assert worker.pbar.n == test_val[0]24def test_function_worker_nonzero_total_warns():25 def not_a_generator():26 return27 with pytest.warns(RuntimeWarning):28 thread_func = qthreading.thread_worker(29 not_a_generator,30 progress={'total': 2},31 start_thread=False,32 )33 thread_func()34def test_worker_may_exceed_total(qtbot):35 test_val = [0]36 def func():37 yield 138 yield 139 def test_yield(v):40 test_val[0] += 141 thread_func = qthreading.thread_worker(42 func,43 connect={'yielded': test_yield},44 progress={'total': 1},45 start_thread=False,46 )47 worker = thread_func()48 with qtbot.waitSignal(worker.yielded):49 worker.start()50 if test_val[0] < 2:51 assert worker.pbar.n == test_val[0]52 else:53 assert worker.pbar.total == 054def test_generator_worker_with_description():55 def func():56 yield 157 thread_func = qthreading.thread_worker(58 func,59 progress={'total': 1, 'desc': 'custom'},60 start_thread=False,61 )62 worker = thread_func()63 assert worker.pbar.desc == 'custom'64def test_function_worker_with_description():65 def func():66 for _ in range(10):67 pass68 thread_func = qthreading.thread_worker(69 func,70 progress={'total': 0, 'desc': 'custom'},71 start_thread=False,72 )73 worker = thread_func()74 assert worker.pbar.desc == 'custom'75def test_generator_worker_with_no_total():76 def func():77 yield 178 thread_func = qthreading.thread_worker(79 func,80 progress=True,81 start_thread=False,82 )83 worker = thread_func()84 assert worker.pbar.total == 085def test_function_worker_with_no_total():86 def func():87 for _ in range(10):88 pass89 thread_func = qthreading.thread_worker(90 func,91 progress=True,92 start_thread=False,93 )94 worker = thread_func()95 assert worker.pbar.total == 096def test_function_worker_0_total():97 def func():98 for _ in range(10):99 pass100 thread_func = qthreading.thread_worker(101 func,102 progress={'total': 0},103 start_thread=False,104 )105 worker = thread_func()106 assert worker.pbar.total == 0107def test_unstarted_worker_no_widget(make_napari_viewer):108 viewer = make_napari_viewer()109 def func():110 for _ in range(5):111 yield112 thread_func = qthreading.thread_worker(113 func,114 progress={'total': 5},115 start_thread=False,116 )117 thread_func()118 assert not bool(119 viewer.window._qt_viewer.window()._activity_dialog.findChildren(120 QtLabeledProgressBar121 )...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!