diff --git a/Chapter02/README.md b/Chapter02/README.md index bb03abc..ddc0e6f 100644 --- a/Chapter02/README.md +++ b/Chapter02/README.md @@ -39,4 +39,14 @@ 4. Tidak boleh meniru logika teman sekelas, harus orisinil logika harus berbeda dengan temannya 5. Tulis pada test_app dengan mengecek isi file yang telah di buat, diubah dan di rename. Nilai di dalam file di assert 6. Pull request maximal jumat jam 11 siang +7. Dikumpulkan dengan cara merge request ke branch 2021, judul merge request diisi nama npm, deskripsi diisi penjelasan jelas bagaimana fungsi fungsi tersebut bekerja. dijelaskan per poin + + +# Tugas pertemuan 7 +1. Buatlah fungsi thread dengan queue dalam file NamaTujuhNPM.py +2. Minimal ada dua fungsi atau kelas(dianjurkan lebih banyak), yaitu threadnya dan fungsi main nya +3. Fungsi tersebut mengimplementasikan web service atau manajemen file atau variabel global +4. Tidak boleh meniru logika teman sekelas, harus orisinil logika harus berbeda dengan temannya +5. Panggil pada test_app fungsi main dari file tersebut. wajib harus ada assert +6. Pull request maximal jumat jam 11 siang 7. Dikumpulkan dengan cara merge request ke branch 2021, judul merge request diisi nama npm, deskripsi diisi penjelasan jelas bagaimana fungsi fungsi tersebut bekerja. dijelaskan per poin \ No newline at end of file diff --git a/Chapter02/Threading_with_queue.py b/Chapter02/Threading_with_queue.py index 5cebee6..aec6ab8 100644 --- a/Chapter02/Threading_with_queue.py +++ b/Chapter02/Threading_with_queue.py @@ -6,6 +6,8 @@ import random + + class Producer(Thread): def __init__(self, queue): @@ -13,29 +15,36 @@ def __init__(self, queue): self.queue = queue def run(self): + global adaantrian + adaantrian = True for i in range(5): item = random.randint(0, 256) self.queue.put(item) print('Producer notify : item N°%d appended to queue by %s\n'\ % (item, self.name)) time.sleep(1) - + self.queue.join()#akan memblokir sampai semua antrian beres + print('selesai semua antrian') class Consumer(Thread): def __init__(self, queue): Thread.__init__(self) self.queue = queue + def run(self): while True: item = self.queue.get() print('Consumer notify : %d popped from queue by %s'\ % (item, self.name)) - self.queue.task_done() + + self.queue.task_done()#penanda per antrian sudah di proses + if __name__ == '__main__': queue = Queue() + adaantrian=False t1 = Producer(queue) t2 = Consumer(queue) @@ -47,7 +56,8 @@ def run(self): t3.start() t4.start() - t1.join() - t2.join() - t3.join() - t4.join() + +# t1.join() +# t2.join() +# t3.join() +# t4.join() diff --git a/Chapter02/WahyuKurniaSariTujuh1184001.py b/Chapter02/WahyuKurniaSariTujuh1184001.py new file mode 100644 index 0000000..5c8c2bf --- /dev/null +++ b/Chapter02/WahyuKurniaSariTujuh1184001.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +""" +Created on Mon Apr 19 20:01:32 2021 + +@author: NITRO 5 ACER +""" + +# -*- coding: utf-8 -*- +""" +Created on Mon Apr 19 19:23:22 2021 + +@author: NITRO 5 ACER +""" + +import queue +import threading +import random +import time + +num_worker_threads = 1 +q = queue.Queue() +threads =[] + +def webservices(): + apiurl = 'https://pokeapi.co/api/v2/pokemon/pikachu' + response = time.get(apiurl) + html = response.json() + print(html["weight"]) + +def run(): + webservices() + +def do_work(item): + print(item) + +def source(): + return range(100) + + +def worker(): + # return True + while True: + item = q.get() + if item is None: + break + do_work(item) + q.task_done() + +def main(): + + for i in range(num_worker_threads): + t = threading.Thread(target=worker) + t.start() + threads.append(t) + + for item in source(): + q.put(item) + + q.join() + + print('stop untuk perhitungan!') + + + for i in range(num_worker_threads): + q.put(None) + + for t in threads: + t.join() + return True diff --git a/test_app.py b/test_app.py index cd08d1d..0f58257 100644 --- a/test_app.py +++ b/test_app.py @@ -402,6 +402,21 @@ def readfile(self,filename): # threadutama.join() # respon=self.readfile('./Chapter02/apigempa.txt') # self.assertNotRegex(respon, "kosong") + # def test_07_idaTujuh1184113(self): + # from Chapter02.IdaTujuh1184113 import idaTujuh1184113,Director + # threadutama = idaTujuh1184113("Thread Utama ", 2,5,5,'ida') + # threaddirector= Director("Thread Director ", 1,'ida') + # threaddirector.start() + # threadutama.start() + # threaddirector.join() + # threadutama.join() + # respon=self.readfile('./Chapter02/ida.txt') + # self.assertNotRegex(respon, "kosong") + + + + + # def test_07_idaTujuh1184113(self): # from Chapter02.IdaTujuh1184113 import idaTujuh1184113,Director @@ -415,6 +430,18 @@ def readfile(self,filename): # self.assertNotRegex(respon, "kosong") + + # def test_07_DiarTujuh1184083(self): + # from Chapter02.DiarTujuh1184083 import DiarQue,DiarTujuh1184083 + # ti = DiarQue("Thread 1 ",'kodepos') + # tl = DiarTujuh1184083("Thread 2",'kodepos') + # ti.start() + # tl.start() + # ti.join() + # tl.join() + # respon=self.readfile('./Chapter02/kodepos') + # self.assertNotRegex(respon, "Gak Boleh Kosong") + #def test_07_DiarTujuh1184083(self): # from Chapter02.DiarTujuh1184083 import DiarQue,DiarTujuh1184083 # ti = DiarQue("Thread 1 ",'kodepos') @@ -426,6 +453,7 @@ def readfile(self,filename): # respon=self.readfile('./Chapter02/kodepos') # self.assertNotRegex(respon, "Gak Boleh Kosong") + #def test_07_idaTujuh1184113(self): # from Chapter02.IdaTujuh1184113 import idaTujuh1184113,Director # threadutama = idaTujuh1184113("Thread Utama ", 2,5,5,'ida') @@ -438,6 +466,16 @@ def readfile(self,filename): # self.assertNotRegex(respon, "kosong") #def test_07_rizaluarditujuh_1184102(self): + # from Chapter02.RizaluardiTujuh1184102 import RizaluardiIms,RizaluardiTujuh1184102 + # nganu1 = RizaluardiIms("Thread 1", 'provinsi') + # nganu2 = RizaluardiTujuh1184102("Thread 2",'provinsi') + # nganu1.start() + # nganu2.start() + # nganu1.join() + # nganu2.join() + # respon=self.readfile('./Chapter02/provinsi') + # self.assertNotRegex(respon, "Jangan kosong datanya") + # from Chapter02.RizaluardiTujuh1184102 import RizaluardiIms,RizaluardiTujuh1184102 # nganu1 = RizaluardiIms("Thread 1", 'provinsi') # nganu2 = RizaluardiTujuh1184102("Thread 2",'provinsi') @@ -447,7 +485,7 @@ def readfile(self,filename): # nganu2.join() # respon=self.readfile('./Chapter02/provinsi') # self.assertNotRegex(respon, "Jangan kosong datanya") - + #def test_07_josuanseftujuh1184091(self): # from Chapter02.JosuansefTujuh_1184091 import Josuansef1184091Tujuh_Producer,Josuansef1184091Tujuh_Consumer # threadjosuaproducer = Josuansef1184091Tujuh_Producer("Thread utama ", 2,5,5,'joss') @@ -482,10 +520,21 @@ def readfile(self,filename): # respon=self.readfile('./Chapter02/ira.txt') # self.assertNotRegex(respon, "kosong") - def test_07_vickyTujuh1184037(self): - from Chapter02.VickyTujuh1184037 import vickysaf - thread1 = vickysaf("Thread Put", "vicky") - thread1.start() - thread1.join() - respon=self.readfile('./Chapter02/vicky') - self.assertNotRegex(respon, "Kosong") \ No newline at end of file + + + + # def test_07_vickyTujuh1184037(self): + # from Chapter02.VickyTujuh1184037 import vickysaf + # thread1 = vickysaf("Thread Put", "vicky") + # thread1.start() + # thread1.join() + # respon=self.readfile('./Chapter02/vicky') + # self.assertNotRegex(respon, "Kosong") + + + def test_07_WahyuKurniaSariTujuh1184001(self): + from Chapter02.WahyuKurniaSariTujuh1184001 import main + response = main() + self.assertEqual(response, True) + +