From 6f93abc516099ad2760a2030e8806f387bd719a8 Mon Sep 17 00:00:00 2001 From: Rolly Maulana Awangga Date: Sat, 17 Apr 2021 14:20:59 +0700 Subject: [PATCH 1/9] tugas 7 --- Chapter02/README.md | 10 ++++++++++ Chapter02/Threading_with_queue.py | 22 ++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Chapter02/README.md b/Chapter02/README.md index bb03abc..834ccab 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(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() From bbae95daf5c73b90a2d8c478e70dda59bcca93ee Mon Sep 17 00:00:00 2001 From: Rolly Maulana Awangga Date: Sat, 17 Apr 2021 14:23:19 +0700 Subject: [PATCH 2/9] update tugas 7 --- Chapter02/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chapter02/README.md b/Chapter02/README.md index 834ccab..ddc0e6f 100644 --- a/Chapter02/README.md +++ b/Chapter02/README.md @@ -44,7 +44,7 @@ # Tugas pertemuan 7 1. Buatlah fungsi thread dengan queue dalam file NamaTujuhNPM.py -2. Minimal ada dua fungsi(dianjurkan lebih banyak), yaitu threadnya dan fungsi main nya +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 From b6d08666e7aef41de013f2a6d840fc4b57373dc1 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 22 Apr 2021 17:21:25 +0700 Subject: [PATCH 3/9] WahyuKurniaSariTujuh1184001 --- Chapter02/WahyuKurniaSariTujuh1184001.py | 82 ++++++++++++++++++++++++ test_app.py | 36 ++++++++--- 2 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 Chapter02/WahyuKurniaSariTujuh1184001.py diff --git a/Chapter02/WahyuKurniaSariTujuh1184001.py b/Chapter02/WahyuKurniaSariTujuh1184001.py new file mode 100644 index 0000000..5f447b7 --- /dev/null +++ b/Chapter02/WahyuKurniaSariTujuh1184001.py @@ -0,0 +1,82 @@ +# -*- 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 +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() + False + # return False +q = queue.Queue() +#def main(): +threads = [] + +for i in range(num_worker_threads): + t = threading.Thread(target=worker) + t.start() + threads.append(t) + + +for item in source(): + q.put(item) + +# block until all tasks are done + q.join() + +print('stop untuk perhitungan!') + +# stop workers +for i in range(num_worker_threads): + q.put(None) + +for t in threads: + t.join() +#return True + + # threads = [] + + # for i in range(4): + # thread = threading.Thread(target=run) + # thread.start() + # threads.append(thread) + + #for t in threads: + # t.join() + # return True + diff --git a/test_app.py b/test_app.py index 8e887f5..019bdb5 100644 --- a/test_app.py +++ b/test_app.py @@ -399,14 +399,30 @@ def readfile(self,nfile): # 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_WahyuKurniaSariTujuh1184001(self): + from Chapter02.WahyuKurniaSariTujuh1184001 import worker + threads = [] + self.assertEqual(threads, True) + + + #self.assertEqual(threads) + + #self.assertNotRegex(threads, "true") + + # self.assertEqual(threads, True) + - 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") \ No newline at end of file + +APP = TestApp() +APP.test_07_WahyuKurniaSariTujuh1184001() \ No newline at end of file From d8e69a0ceba6051c7e9a05ea6893314e2a150d85 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 23 Apr 2021 08:41:10 +0700 Subject: [PATCH 4/9] Update WahyuKurniaSariTujuh1184001 --- Chapter02/WahyuKurniaSariTujuh1184001.py | 29 +++++++++-------------- test_app.py | 30 ++++++++++-------------- 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/Chapter02/WahyuKurniaSariTujuh1184001.py b/Chapter02/WahyuKurniaSariTujuh1184001.py index 5f447b7..8985192 100644 --- a/Chapter02/WahyuKurniaSariTujuh1184001.py +++ b/Chapter02/WahyuKurniaSariTujuh1184001.py @@ -11,18 +11,23 @@ @author: NITRO 5 ACER """ + import queue import threading import random import time num_worker_threads = 1 + + def webservices(): apiurl = 'https://pokeapi.co/api/v2/pokemon/pikachu' response = time.get(apiurl) html = response.json() print(html["weight"]) + + def run(): webservices() @@ -34,17 +39,18 @@ def source(): def worker(): - return True + # return True while True: item = q.get() if item is None: break do_work(item) q.task_done() - False - # return False + return True + + + q = queue.Queue() -#def main(): threads = [] for i in range(num_worker_threads): @@ -56,27 +62,14 @@ def worker(): for item in source(): q.put(item) -# block until all tasks are done q.join() print('stop untuk perhitungan!') -# stop workers + for i in range(num_worker_threads): q.put(None) for t in threads: t.join() -#return True - - # threads = [] - - # for i in range(4): - # thread = threading.Thread(target=run) - # thread.start() - # threads.append(thread) - - #for t in threads: - # t.join() - # return True diff --git a/test_app.py b/test_app.py index 9b2477b..1d45de5 100644 --- a/test_app.py +++ b/test_app.py @@ -413,10 +413,9 @@ def readfile(self,filename): # respon=self.readfile('./Chapter02/ida.txt') # self.assertNotRegex(respon, "kosong") - def test_07_WahyuKurniaSariTujuh1184001(self): - from Chapter02.WahyuKurniaSariTujuh1184001 import worker - response = worker() - self.assertEqual(response, True) + + + # def test_07_idaTujuh1184113(self): @@ -441,9 +440,9 @@ def test_07_WahyuKurniaSariTujuh1184001(self): # respon=self.readfile('./Chapter02/ida.txt') # self.assertNotRegex(respon, "kosong") - # def test_07_rizaluarditujuh_1184102(self): + #def test_07_rizaluarditujuh_1184102(self): # from Chapter02.RizaluardiTujuh1184102 import RizaluardiIms,RizaluardiTujuh1184102 - # nganu1 = RizaluardiIms("Thread 1", 'provinsi') + # nganu1 = RizaluardiIms("Thread 1", 'provinsi') # nganu2 = RizaluardiTujuh1184102("Thread 2",'provinsi') # nganu1.start() # nganu2.start() @@ -475,18 +474,6 @@ def test_07_WahyuKurniaSariTujuh1184001(self): # self.assertNotRegex(respon, "kosong") - - # def test_07_iraTujuh1184024(self): - # from Chapter02.IraTujuh1184024 import iraTujuh1184024,iraHandlingFile - # threadutama = iraTujuh1184024("Thread Utama ", 2,5,5,'ira') - # threadhandling= iraHandlingFile("Thread handling File ", 1,'ira') - # threadutama.start() - # threadhandling.join() - # threadutama.join() - # respon=self.readfile('./Chapter02/ira.txt') - # self.assertNotRegex(respon, "kosong") - - #def test_07_iraTujuh1184024(self): # from Chapter02.IraTujuh1184024 import iraTujuh1184024,iraHandlingFile # threadutama = iraTujuh1184024("Thread Utama ", 2,5,5,'ira') @@ -497,3 +484,10 @@ def test_07_WahyuKurniaSariTujuh1184001(self): # threadutama.join() # respon=self.readfile('./Chapter02/ira.txt') # self.assertNotRegex(respon, "kosong") + + def test_07_WahyuKurniaSariTujuh1184001(self): + from Chapter02.WahyuKurniaSariTujuh1184001 import worker + response = worker() + self.assertEqual(response, True) + + From a4ab8f56adcc0b6bda1c2cee909ba1a2ff230ca1 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 23 Apr 2021 08:46:18 +0700 Subject: [PATCH 5/9] Update WahyuKurniaSariTujuh1184001 --- FETCH_HEAD | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 FETCH_HEAD diff --git a/FETCH_HEAD b/FETCH_HEAD new file mode 100644 index 0000000..e69de29 From afe72026b91f7a0cd994b10b918aed2d2213f32b Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 23 Apr 2021 09:19:10 +0700 Subject: [PATCH 6/9] upload --- test_app.py | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/test_app.py b/test_app.py index 97cef80..3617eec 100644 --- a/test_app.py +++ b/test_app.py @@ -430,16 +430,16 @@ 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') + # 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_idaTujuh1184113(self): # from Chapter02.IdaTujuh1184113 import idaTujuh1184113,Director @@ -453,7 +453,6 @@ def test_07_DiarTujuh1184083(self): # self.assertNotRegex(respon, "kosong") #def test_07_rizaluarditujuh_1184102(self): -<<<<<<< HEAD # from Chapter02.RizaluardiTujuh1184102 import RizaluardiIms,RizaluardiTujuh1184102 # nganu1 = RizaluardiIms("Thread 1", 'provinsi') # nganu2 = RizaluardiTujuh1184102("Thread 2",'provinsi') @@ -463,7 +462,7 @@ def test_07_DiarTujuh1184083(self): # 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') @@ -473,8 +472,7 @@ def test_07_DiarTujuh1184083(self): # nganu2.join() # respon=self.readfile('./Chapter02/provinsi') # self.assertNotRegex(respon, "Jangan kosong datanya") ->>>>>>> f941f1833309573252b87b10d5c92603f2d3da2e - + #def test_07_josuanseftujuh1184091(self): # from Chapter02.JosuansefTujuh_1184091 import Josuansef1184091Tujuh_Producer,Josuansef1184091Tujuh_Consumer # threadjosuaproducer = Josuansef1184091Tujuh_Producer("Thread utama ", 2,5,5,'joss') @@ -508,13 +506,11 @@ def test_07_DiarTujuh1184083(self): # threadutama.join() # respon=self.readfile('./Chapter02/ira.txt') # self.assertNotRegex(respon, "kosong") -<<<<<<< HEAD - + def test_07_WahyuKurniaSariTujuh1184001(self): from Chapter02.WahyuKurniaSariTujuh1184001 import worker response = worker() self.assertEqual(response, True) -======= ->>>>>>> f941f1833309573252b87b10d5c92603f2d3da2e + From d0e2575676e962ef999ca97f802b80a165928296 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 23 Apr 2021 09:45:33 +0700 Subject: [PATCH 7/9] upload --- Chapter02/WahyuKurniaSariTujuh1184001.py | 48 +++++++++++------------- test_app.py | 5 +-- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/Chapter02/WahyuKurniaSariTujuh1184001.py b/Chapter02/WahyuKurniaSariTujuh1184001.py index 8985192..5c8c2bf 100644 --- a/Chapter02/WahyuKurniaSariTujuh1184001.py +++ b/Chapter02/WahyuKurniaSariTujuh1184001.py @@ -18,7 +18,8 @@ import time num_worker_threads = 1 - +q = queue.Queue() +threads =[] def webservices(): apiurl = 'https://pokeapi.co/api/v2/pokemon/pikachu' @@ -26,8 +27,6 @@ def webservices(): html = response.json() print(html["weight"]) - - def run(): webservices() @@ -46,30 +45,25 @@ def worker(): break do_work(item) q.task_done() - return True +def main(): - -q = queue.Queue() -threads = [] - -for i in range(num_worker_threads): - t = threading.Thread(target=worker) - t.start() - threads.append(t) + for i in range(num_worker_threads): + t = threading.Thread(target=worker) + t.start() + threads.append(t) + + for item in source(): + q.put(item) - -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() - + 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 3617eec..de4a3e9 100644 --- a/test_app.py +++ b/test_app.py @@ -508,9 +508,8 @@ def readfile(self,filename): # self.assertNotRegex(respon, "kosong") def test_07_WahyuKurniaSariTujuh1184001(self): - from Chapter02.WahyuKurniaSariTujuh1184001 import worker - response = worker() + from Chapter02.WahyuKurniaSariTujuh1184001 import main + response = main() self.assertEqual(response, True) - From 683ba5be49b2696ae9493a1e3d22829ba1d7b217 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 23 Apr 2021 09:51:44 +0700 Subject: [PATCH 8/9] upload --- FETCH_HEAD | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 FETCH_HEAD diff --git a/FETCH_HEAD b/FETCH_HEAD deleted file mode 100644 index e69de29..0000000 From 6c6158e3bac692f86528252c2e9ff16f408dbbd5 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 23 Apr 2021 11:08:14 +0700 Subject: [PATCH 9/9] upload --- test_app.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/test_app.py b/test_app.py index 5ee6275..0f58257 100644 --- a/test_app.py +++ b/test_app.py @@ -430,7 +430,7 @@ def readfile(self,filename): # self.assertNotRegex(respon, "kosong") -<<<<<<< HEAD + # def test_07_DiarTujuh1184083(self): # from Chapter02.DiarTujuh1184083 import DiarQue,DiarTujuh1184083 # ti = DiarQue("Thread 1 ",'kodepos') @@ -441,7 +441,7 @@ def readfile(self,filename): # 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') @@ -452,7 +452,7 @@ def readfile(self,filename): # tl.join() # respon=self.readfile('./Chapter02/kodepos') # self.assertNotRegex(respon, "Gak Boleh Kosong") ->>>>>>> 64315001c7264e2c9bc100e1b513be3a7f29e78b + #def test_07_idaTujuh1184113(self): # from Chapter02.IdaTujuh1184113 import idaTujuh1184113,Director @@ -520,19 +520,21 @@ def readfile(self,filename): # respon=self.readfile('./Chapter02/ira.txt') # self.assertNotRegex(respon, "kosong") -<<<<<<< HEAD + + + + # 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) -======= - 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") ->>>>>>> 64315001c7264e2c9bc100e1b513be3a7f29e78b