Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
magicCards
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Thomas Letzgus
magicCards
Commits
a1cf3d6e
Commit
a1cf3d6e
authored
2 years ago
by
Thomas Letzgus
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
062a32fd
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
producer.py
+37
-0
37 additions, 0 deletions
producer.py
with
37 additions
and
0 deletions
producer.py
0 → 100644
+
37
−
0
View file @
a1cf3d6e
from
car
import
Car
from
buffer
import
Buffer
import
threading
import
random
import
time
class
Producer
:
def
__init__
(
self
,
buffer_obj
):
self
.
buffer
=
buffer_obj
def
produce
(
self
):
while
True
:
time
.
sleep
(
random
.
uniform
(
0.5
,
2.0
))
# Random interval between checks
with
self
.
buffer
.
condition
:
while
self
.
buffer
.
full
():
self
.
buffer
.
condition
.
wait
()
# Wait until buffer is not full
car_id
=
random
.
randint
(
1
,
100
)
# Generate a random car ID
car
=
Car
(
car_id
)
self
.
buffer
.
push
(
car
)
print
(
"
Producer: Car
"
,
car
.
get_car_id
(),
"
parked
"
)
if
self
.
buffer
.
empty
():
# Buffer was empty before parking, wake up all sleeping consumers
self
.
buffer
.
condition
.
notify_all
()
# Example usage:
buffer
=
Buffer
(
size
=
10
)
# Assuming buffer is already initialized with a size of 10
producer
=
Producer
(
buffer
)
# Create and start the producer thread
producer_thread
=
threading
.
Thread
(
target
=
producer
.
produce
)
producer_thread
.
start
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment