Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
trv-crawl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
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
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nils Breyer
trv-crawl
Commits
86465ebc
Commit
86465ebc
authored
2 years ago
by
Nils Breyer
Browse files
Options
Downloads
Patches
Plain Diff
write lock to prevent simulatenous writes to csv file
parent
93eb4b21
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.md
+5
-1
5 additions, 1 deletion
CHANGELOG.md
crawl.py
+1
-1
1 addition, 1 deletion
crawl.py
crawler/__init__.py
+2
-1
2 additions, 1 deletion
crawler/__init__.py
crawler/trafikverket.py
+18
-8
18 additions, 8 deletions
crawler/trafikverket.py
with
26 additions
and
11 deletions
CHANGELOG.md
+
5
−
1
View file @
86465ebc
# Changelog
## 1.1.2 (2022-01-26)
*
FIX: Simultaneous writes to csv can cause invalid format in output file
## 1.1.1 (2022-01-13)
*
FIX:
Initialize new connection
if status file unreadable
*
FIX:
Connection not initialized
if status file unreadable
## 1.1.0 (2022-01-13)
...
...
This diff is collapsed.
Click to expand it.
crawl.py
+
1
−
1
View file @
86465ebc
...
...
@@ -12,7 +12,7 @@ from crawler import StreamCrawler
import
crawler.trafikverket
VERSION
=
"
1.1.
1
"
VERSION
=
"
1.1.
2
"
parser
=
argparse
.
ArgumentParser
(
description
=
"
Crawler for Trafikverket
'
s Trafikinfo API - version
"
+
VERSION
)
...
...
This diff is collapsed.
Click to expand it.
crawler/__init__.py
+
2
−
1
View file @
86465ebc
import
shutil
from
threading
import
Event
,
Timer
,
Thread
from
threading
import
Event
,
Timer
,
Thread
,
Lock
from
queue
import
Queue
import
os
import
requests
...
...
@@ -47,6 +47,7 @@ class Crawler:
message_webhook_url
=
None
messages
=
Queue
()
authkey
=
""
write_lock
=
Lock
()
DEBUG
=
False
def
__init__
(
self
,
url
,
path
,
authkey
,
version
=
VERSION
,
output_version
=
OUTPUT_VERSION
):
...
...
This diff is collapsed.
Click to expand it.
crawler/trafikverket.py
+
18
−
8
View file @
86465ebc
...
...
@@ -219,8 +219,10 @@ class DailyTrainAnnouncementCrawler (crawler.PeriodicCrawler):
self
.
log_exception
(
e
)
#write to csv
data
.
to_csv
(
filename
,
index
=
False
)
self
.
writes
+=
1
with
self
.
write_lock
:
data
.
to_csv
(
filename
,
index
=
False
)
self
.
writes
+=
1
if
self
.
DEBUG
:
self
.
log_info
(
"
Written {rows} rows.
"
.
format
(
rows
=
len
(
data
)))
except
Exception
as
e
:
...
...
@@ -281,8 +283,10 @@ class DailyTrainMessageCrawler (crawler.PeriodicCrawler):
self
.
log_exception
(
e
)
#write to csv
data
.
to_csv
(
filename
,
index
=
False
)
self
.
writes
+=
1
with
self
.
write_lock
:
data
.
to_csv
(
filename
,
index
=
False
)
self
.
writes
+=
1
if
self
.
DEBUG
:
self
.
log_info
(
"
Written {rows} rows.
"
.
format
(
rows
=
len
(
data
)))
except
Exception
as
e
:
...
...
@@ -331,11 +335,14 @@ class StreamTrainAnnouncementCrawler (crawler.StreamCrawler):
data
.
insert
(
loc
=
0
,
column
=
"
TimeReceived
"
,
value
=
len
(
data
)
*
[
format_datetime
(
datetime
.
datetime
.
now
(),
utc
=
True
)])
if
self
.
DEBUG
:
self
.
log_info
(
"
Received {rows} rows.
"
.
format
(
rows
=
len
(
data
)))
#write to csv
data
.
to_csv
(
self
.
csvfile
,
index
=
False
,
mode
=
'
a
'
,
header
=
(
not
os
.
path
.
exists
(
self
.
csvfile
)))
with
self
.
write_lock
:
data
.
to_csv
(
self
.
csvfile
,
index
=
False
,
mode
=
'
a
'
,
header
=
(
not
os
.
path
.
exists
(
self
.
csvfile
)))
self
.
writes
+=
1
if
self
.
DEBUG
:
self
.
log_info
(
"
Written {rows} rows.
"
.
format
(
rows
=
len
(
data
)))
self
.
writes
+=
1
except
Exception
as
e
:
self
.
log_exception
(
e
)
...
...
@@ -376,11 +383,14 @@ class StreamTrainMessageCrawler (crawler.StreamCrawler):
if
self
.
DEBUG
:
self
.
log_info
(
"
Received {rows} rows.
"
.
format
(
rows
=
len
(
data
)))
#write to csv
data
.
to_csv
(
self
.
csvfile
,
index
=
False
,
mode
=
'
a
'
,
header
=
(
not
os
.
path
.
exists
(
self
.
csvfile
)))
with
self
.
write_lock
:
data
.
to_csv
(
self
.
csvfile
,
index
=
False
,
mode
=
'
a
'
,
header
=
(
not
os
.
path
.
exists
(
self
.
csvfile
)))
self
.
writes
+=
1
if
self
.
DEBUG
:
self
.
log_info
(
"
Written {rows} rows.
"
.
format
(
rows
=
len
(
data
)))
self
.
writes
+=
1
except
Exception
as
e
:
self
.
log_exception
(
e
)
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