mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-17 22:20:22 -04:00
add all concurrence examples
This commit is contained in:
parent
2333b724ec
commit
5224dc91ca
12 changed files with 49 additions and 2 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import random
|
import random
|
||||||
import logging
|
import logging
|
||||||
import concurrent.futures
|
import concurrent.futures
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from gevent.pool import Pool
|
from gevent.pool import Pool
|
||||||
from gevent import monkey
|
from gevent import monkey
|
||||||
|
@ -20,4 +22,3 @@ pool.join()
|
||||||
end_time = time.time()
|
end_time = time.time()
|
||||||
|
|
||||||
print("Time for GreenSquirrel: %ssecs" % (end_time - start_time))
|
print("Time for GreenSquirrel: %ssecs" % (end_time - start_time))
|
||||||
g
|
|
|
@ -1,6 +1,8 @@
|
||||||
import multiprocessing
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import random
|
import random
|
||||||
|
import multiprocessing
|
||||||
|
|
||||||
|
|
||||||
def worker(n):
|
def worker(n):
|
||||||
|
|
10
Concurrence_examples/pool_example.py
Normal file
10
Concurrence_examples/pool_example.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from multiprocessing import Pool
|
||||||
|
|
||||||
|
def f(x):
|
||||||
|
return x*x
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
p = Pool(5)
|
||||||
|
print(p.map(f, [1, 2, 3]))
|
|
@ -1,3 +1,5 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
x = 0
|
x = 0
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
counter = 0
|
counter = 0
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import random
|
import random
|
||||||
import threading
|
import threading
|
||||||
|
|
18
Concurrence_examples/threadpool_example.py
Normal file
18
Concurrence_examples/threadpool_example.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from time import sleep
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
|
||||||
|
|
||||||
|
def return_after_5_secs(message):
|
||||||
|
sleep(5)
|
||||||
|
return message
|
||||||
|
|
||||||
|
pool = ThreadPoolExecutor(3)
|
||||||
|
|
||||||
|
|
||||||
|
future = pool.submit(return_after_5_secs, ("hello"))
|
||||||
|
print(future.done())
|
||||||
|
sleep(5)
|
||||||
|
print(future.done())
|
||||||
|
print(future.result())
|
|
@ -1,3 +1,5 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue