mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-05-18 06:30:23 -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 logging
|
||||
import concurrent.futures
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import time
|
||||
import threading
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import time
|
||||
import threading
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
from gevent.pool import Pool
|
||||
from gevent import monkey
|
||||
|
@ -20,4 +22,3 @@ pool.join()
|
|||
end_time = time.time()
|
||||
|
||||
print("Time for GreenSquirrel: %ssecs" % (end_time - start_time))
|
||||
g
|
|
@ -1,6 +1,8 @@
|
|||
import multiprocessing
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
import random
|
||||
import multiprocessing
|
||||
|
||||
|
||||
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
|
||||
|
||||
x = 0
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import threading
|
||||
|
||||
counter = 0
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
import random
|
||||
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
|
||||
from queue import Queue
|
||||
from threading import Thread
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
import threading
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue