generated from ossf/project-template
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #532 from myteron/pyDoc2Github_multiprocessing_mul…
…tithreading PyDoc2GitHub - Introduction to Multithreading and Multiprocessing in …
- Loading branch information
Showing
15 changed files
with
468 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-665/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# CWE-665: Improper Initialization | ||
|
||
> [!NOTE] | ||
> Prerequisite to understand this page: | ||
> [Intro to multiprocessing and multithreading](../../Intro_to_multiprocessing_and_multithreading/readme.md) | ||
## Introduction | ||
|
||
> [!NOTE] | ||
> Placeholder page for links, content still need to be moved into github |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...Secure-Coding-Guide-for-Python/Intro_to_multiprocessing_and_multithreading/compliant01.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
""" Compliant Code Example """ | ||
|
||
import time | ||
from multiprocessing import Process | ||
|
||
|
||
def waste_time(t: int): | ||
for _ in range(t): | ||
_ += 1 | ||
|
||
|
||
BIG_NUMBER = 100000000 | ||
start = time.time() | ||
waste_time(BIG_NUMBER) | ||
end = time.time() | ||
print(f"Time taken when executing sequentially (in seconds): {end - start}") | ||
p1 = Process(target=waste_time, args=(BIG_NUMBER // 2,)) | ||
p2 = Process(target=waste_time, args=(BIG_NUMBER // 2,)) | ||
start = time.time() | ||
p1.start() | ||
p2.start() | ||
p1.join() | ||
p2.join() | ||
end = time.time() | ||
print(f"Time taken when executing in 2 processes (in seconds): {end - start}") |
17 changes: 17 additions & 0 deletions
17
...Secure-Coding-Guide-for-Python/Intro_to_multiprocessing_and_multithreading/compliant02.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
""" Compliant Code Example """ | ||
import asyncio | ||
|
||
|
||
async def func(x: int): | ||
print(f"{x} - start") | ||
await asyncio.sleep(1) | ||
print(f"{x} - end") | ||
|
||
|
||
async def run_async(): | ||
await asyncio.gather(func(1), func(2), func(3)) | ||
|
||
|
||
asyncio.run(run_async()) |
23 changes: 23 additions & 0 deletions
23
docs/Secure-Coding-Guide-for-Python/Intro_to_multiprocessing_and_multithreading/example01.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
""" Non-compliant Code Example """ | ||
|
||
import threading | ||
|
||
|
||
def critical_func(x: str, l: threading.Lock): | ||
print(f"{x}: performing regular operations") | ||
with l: | ||
print(f"{x}: entered critical section") | ||
i = 0 | ||
for _ in range(100000): | ||
i += 1 | ||
print(f"{x}: exiting critical section") | ||
print(f"{x}: finished") | ||
|
||
|
||
lock = threading.Lock() | ||
t1 = threading.Thread(target=critical_func, args=("A", lock)) | ||
t2 = threading.Thread(target=critical_func, args=("B", lock)) | ||
t1.start() | ||
t2.start() |
Binary file added
BIN
+37.1 KB
...Coding-Guide-for-Python/Intro_to_multiprocessing_and_multithreading/image01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.8 KB
...Coding-Guide-for-Python/Intro_to_multiprocessing_and_multithreading/image02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions
17
...ure-Coding-Guide-for-Python/Intro_to_multiprocessing_and_multithreading/noncompliant01.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
""" Non-compliant Code Example """ | ||
import threading | ||
|
||
|
||
def wait_for_other(x: str, other: threading.Thread): | ||
print(f"{x}: waiting for other") | ||
other.join() | ||
print(f"{x}: finished waiting") | ||
|
||
|
||
t = threading.Thread(target=wait_for_other, args=("A", threading.current_thread())) | ||
t.start() | ||
print("B: waiting for other") | ||
t.join() | ||
print("B: finished waiting") |
26 changes: 26 additions & 0 deletions
26
...ure-Coding-Guide-for-Python/Intro_to_multiprocessing_and_multithreading/noncompliant02.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
""" Non-compliant Code Example """ | ||
import time | ||
from threading import Thread | ||
|
||
|
||
def waste_time(t: int): | ||
for _ in range(t): | ||
_ += 1 | ||
|
||
|
||
BIG_NUMBER = 100000000 | ||
start = time.time() | ||
waste_time(BIG_NUMBER) | ||
end = time.time() | ||
print(f"Time taken when executing sequentially (in seconds): {end - start}") | ||
t1 = Thread(target=waste_time, args=(BIG_NUMBER // 2,)) | ||
t2 = Thread(target=waste_time, args=(BIG_NUMBER // 2,)) | ||
start = time.time() | ||
t1.start() | ||
t2.start() | ||
t1.join() | ||
t2.join() | ||
end = time.time() | ||
print(f"Time taken when executing in 2 threads (in seconds): {end - start}") |
25 changes: 25 additions & 0 deletions
25
...ure-Coding-Guide-for-Python/Intro_to_multiprocessing_and_multithreading/noncompliant03.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# SPDX-FileCopyrightText: OpenSSF project contributors | ||
# SPDX-License-Identifier: MIT | ||
""" Non-compliant Code Example """ | ||
import time | ||
from threading import Thread | ||
|
||
|
||
def waste_time(t: float): | ||
time.sleep(t) | ||
|
||
|
||
WAIT_TIME = 4 | ||
start = time.time() | ||
waste_time(WAIT_TIME) | ||
end = time.time() | ||
print(f"Time taken when executing sequentially (in seconds): {end - start}") | ||
t1 = Thread(target=waste_time, args=(WAIT_TIME // 2,)) | ||
t2 = Thread(target=waste_time, args=(WAIT_TIME // 2,)) | ||
start = time.time() | ||
t1.start() | ||
t2.start() | ||
t1.join() | ||
t2.join() | ||
end = time.time() | ||
print(f"Time taken when executing in 2 threads (in seconds): {end - start}") |
Oops, something went wrong.